Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Parse XML string: need to return every individual tag data

795412Feb 23 2010 — edited Feb 23 2010
XML String:
<data>
<person>
<name>Mark</name>
</person>
<person>
<name>John</name>
</person>
</data>

Now, I am using DOM to parse the string to retrieve the names. using StringBuffer and append, I retrieve all the names tied together, in a String like String result = MarkJohn, and this is what returned to the calling program.

Is there any method, by what, I can seperate Mark and John and send them to the calling program.

Calling from another class:


String result = ParseXMLString.ParseXML(input);


System.+out+.println("NAME IS: "+result);


This prints all the names together.

My Code in Class ParseXMLString, under method ParseXML

String result = null;


StringBuffer result_bfr = new StringBuffer();


try {


DocumentBuilderFactory dbf =


DocumentBuilderFactory.+newInstance+();


DocumentBuilder db = dbf.newDocumentBuilder();


InputSource is = new InputSource();


is.setCharacterStream(*new* StringReader(xmlRecords));





Document doc = db.parse(is);


NodeList nodes = doc.getElementsByTagName("Sensor");





// iterate the employees


for (*int* i = 0; i < nodes.getLength(); i++)


{


Element element = (Element) nodes.item(i);





NodeList name = element.getElementsByTagName("name");


Element line = (Element) name.item(0);


//System.out.println("Name: " + getCharacterDataFromElement(line));


// result_bfr.append(i);


result_bfr.append(+getCharacterDataFromElement+(line));


}


result = result_bfr.toString();


_}_

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 23 2010
Added on Feb 23 2010
4 comments
349 views