XML Parsing and creating objects..

I need to create objects with the value present in the XML file under a specific tag.

E.g.:

XML file:

- - - - -

<employees.

><employee>

<name>

<first>Chandan</first>

<last>Sharma</last>

</name>

<empid>1212</empid>

<dept>Telecom</dept>

</employee>

<employee>

<name>

<fitst>First</first>

<last>Last</last>

</name>

<empid>100</empid>

<dept>Dept</dept>

</employee>

</employees>

There may be tags other than <employees> in the XML file.

I need to parse this file and create objects of class

Employees{

String firstName,lastName,empid,dept;

}

-

Please help me.

As I am new with this, it would be better if you provide me some with code.

Thanks & Regards,

Chandan Sharma

[1027 byte] By [chanda.sharmaa] at [2007-10-3 3:26:30]
# 1

in the sample below msg is of type String

and contains the xml message.

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = builderFactory.newDocumentBuilder();

InputSource is = new InputSource(new StringReader(msg));

Document document = builder.parse(is);

document.getDocumentElement().normalize();

Element rootElement = document.getDocumentElement();

then

NodeList l = rootNode.getElementsByTagName("employee");

to get a list of all nodes of type employee

this is a good point to start and you should be able to finish the aork.

Marc

guerrinma at 2007-7-14 21:19:41 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Hi,May be you can build an schema and generate the java classes with jaxb. After you'll be able to read and create objects from xml files.
WuWeia at 2007-7-14 21:19:41 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Thanks MarkHope this is going to work definitly, few more questions..Is there any way to do the same thing using SAX.What would be a better solution, SAX or DOM, if the XML file is very largeRegards,Chandan Sharma
chanda.sharmaa at 2007-7-14 21:19:41 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
Hi WuWei,Can you please elaborate onhow to use JAXBThanks & Regards,Chandan Sharma
chanda.sharmaa at 2007-7-14 21:19:41 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

You can od it using SAX of course but the implementation is completely different. As I haven't used any SAX approach for many years, I can't provide you any example but you should find many of them either with google or by downloading any java implementation (like Xerces, Xalan...)

The better solution depends on your needs. That's the standard answer, I know :)

when you use DOM, you load the entire content of the file in the memory. and then you travel through the JAVA structure that has been created by using the methods I gave you.

When you use SAX, you define a listener for your parser that will be called any time the parser finds something interesting. a new node, a new attribute, a new node content... sometimes it's a pain to implement.

In my opinion if your application will only handle one message at a time, you should keep the DOM approach because I don't think there will be any big difference in time processing. On the other hand if your application will potentially manage multiple messages at the same time, then your computer memory can be overloaded by a DOM approach and the you will face bad performances, in such case use the SAX approach.

as a conclusion, the best way to make a decision is to run benchmarks. good luck :)

Marc

guerrinma at 2007-7-14 21:19:41 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

SAX gives best performance ! ... both, with short and large document.

DOM: parse, load on memory all XML structure, afer that you iterate DOM nodes for build your Java Objects Model ...

SAX: parse and create your Java Object Model at same time. (And validate againts XSD Schemas at same time if you like).

Rulasa at 2007-7-14 21:19:41 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...