Parse XML string: need to return every individual tag data
795412Feb 23 2010 — edited Feb 23 2010XML 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();
_}_