Helpful Information
 
 
Category: BlackBerry Development
What's wrong with this simple xml parsing code?

Here is my simple XML and simple XML parser:



public Document loadXMLFromString(String xml1) throws Exception { String xml = "<root>"; xml = xml + "<returnValue>1</returnValue></root>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8")); Document doc = db.parse(is); doc.normalize(); Dialog.alert("# of Nodes found: " + doc.getElementsByTagName("returnValue").getLength()); Dialog.alert("Node sads: " + doc.getElementsByTagName("returnValue").item(0).getTextContent()); Dialog.alert("Node type: " + doc.getElementsByTagName("returnValue").item(0).getNodeType()); return doc; }




I am trying to parse an xml in my BB app. The xml is very simple and returnValue node contains '1' as it's content. I am getting correct number of nodes from doc.getElementsByTagName("returnValue").getLength());
But I get 'null' when I try to get the content of node by calling:
doc.getElementsByTagName("returnValue").item(0).getTextContent());

PLease please let me know whats wrong with this code??

I guess the code above is not clearly visible ..
Here is the code again:

public Document loadXMLFromString(String xml1) throws Exception
{
String xml = "<root>";
xml = xml + "<returnValue>1</returnValue></root>";


DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
Document doc = db.parse(is);
doc.normalize();

Dialog.alert("# of Nodes found: " + doc.getElementsByTagName("returnValue").getLength());
Dialog.alert("Node sads: " + doc.getElementsByTagName("returnValue").item(0).getTextContent());
Dialog.alert("Node type: " + doc.getElementsByTagName("returnValue").item(0).getNodeType());

return doc;
}










privacy (GDPR)