How to loop through many XML messages and parse them ?

Hi All

I have been trying very hard to loop through many XML messages and process each of them. Let me first explain the problem -

Suppose I have the following String -

<xyz>

....

<abc>happy</abc>

....

</xyz>

<xyz>

....

<abc>new</abc>

....

<xyz>

<xyz>

....

<abc>year</abc>

....

</xyz>

I have to process each message within the <xyz></xyz> tag and find the falue of <abc> element (happy, new and year).

The extraction of <abc> value is very simple, I am using SAX parser's startElement() method to check every element's name and if the element's name is <abc> pick up the value. But I am not able to loop through the different messages within the <xyz></xyz> tag.

I am thinking of using another DOM parser -

DOMParser domParser = new DOMParser();

StringReader rdr = new StringReader(inputXML);

InputSource src = new InputSource(rdr);

domParser.parse(src);

Document doc = domParser.getDocument();

NodeList nodeList = doc.getElementsByTagName("xyz");

Now I can loop through this nodeList, but not able to. Is using the DOM parser and NodeList the preferable way of lopping through the messages, then how I can loop through ? Or is there any other way ?

I have been trying on this for quite a few days, but not able to. Can you please help me out ?

Thanking you in advance ....

Nirmalya Sinha

[1592 byte] By [nsinha1974a] at [2007-11-26 13:54:38]
# 1
Hi, Try using the SAX reader from the dom4j package. The document object that you receive contains methods for getting the root elements and with that you can traverse down to the sub elements of it. Hope this was of some help.
keyboardsamuraia at 2007-7-8 1:33:20 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...