I am trying to parse a few specific parts of an xml doc. I am looking at pulling the data out of the analysis section, I need the warnings, errors, passes and I need to go into each of the sections () and get the result and result level and the text for example in this "ERROR" I would need to get level of error and the text "ERROR".
<document>
<configuration>
</configuration>
<data>
</data>
<analysis warnings="5" errors="3" information="0" passed="false">
<files>
</files>
<results>
<form>
<section number="0">
<result level="error">ERROR</result>
<result level="error">ERROR</result>
<result level="error">ERROR</result>
<result level="warning">Warning</result>
<result level="warning">Warning</result>
</section>
<section number="1">
<result level="warning">WARNING</result>
</section>
<section number="2">
<result level="warning">WARNING</result>
<result level="warning">WARNING</result>
</section>
</form>
</results>
</analysis>
</document>
I have the following code:
public void ProcessXMLFromPath(String path) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(path);
NodeList nodeList = document.getDocumentElement().getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
System.out.println(node.getAttributes().toString());
NodeList childNodes = node.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node cNode = childNodes.item(j);
if (cNode instanceof Element) {
System.out.println(cNode.getNodeName().toString());
if(cNode.getNodeName().toString() == "analysis")
{
String content = cNode.getLastChild().getTextContent().trim();
System.out.println(content);
//I thought this would print the children under the analysis section to the screen but I was mistaken. It does however make it to this point.
}
}
}
}
}
}
The only thing I'm getting to print to my console is:
configuration
data
analysis
Any help would be greatly appreciated!