I have an XML document that looks like this:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="/resources/transform.xslt"?>
<map name="response">
<prop name="numConsumers">87</prop>
<prop name="numOutEventsQueued">17</prop>
<prop name="numOutEventsUnAcked">2131</prop>
<prop name="numOutEventsSent">538108577</prop>
</map>
I get the response as a string, so I parse it to XML and try to extract numConsumers (the value 87):
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xml)));
NodeList nodeList = doc.getChildNodes();
for(int i = 0; i < nodeList.getLength(); i++) {
String numConsumers = nodeList.item(i).getTextContent();
}
But the thing is, when i = 0, numConsumers is type="text/xsl" href="/resources/transform.xslt" and when i = 1, it is all the values of the child nodes, concatenated. What am I doing wrong? I'm aware that the current code is not explicitly looking for the node I want, but at this point I'm just trying to see if I can get to any of the nodes.
<?xml-stylesheet>attributes. You should follow some tutorial on how to read a complete XML first. Does could be tricky at first. Here is a good start Java XML