Compare child nodes to control value while parsing XML

rainsonb

New Member
I have an XML file organized like this, items under each node are always in alphabetical order:\[code\]<xml> <node id="2"> <jack>Jack wrote this.</jack> <john>John wrote this.</john> </node> <node id="4"> <jack>Jack wrote this.</jack> <jill>Jill wrote this.</jill> </node> <node id="9"> <jack>Jack wrote this.</jack> <james>James wrote this.</james> <jill>Jill wrote this.</jill> <john>John wrote this.</john> </node></xml>\[/code\]As you can see, not all the names are under each node. For example, in \[code\]<node id="4">\[/code\], John and James did not write anything. For the above example, I'd like my program to return something like this:\[code\]James did not write 2, 4Jill did not write 2John did not write 4\[/code\]I need to keep track of who didn't write what. I am currently parsing the document like this:\[code\]private static String getTagValue(final Element element){ String theId=""; if (element.getTagName().startsWith("node")){ theId = element.getAttribute("id"); return theId; } return theId;}private static void readXML(String fileName){ for (int index = 0; index < nodeList.getLength(); index++){ Node node = nodeList.item(index); Element element = (Element) node; if (node.getNodeType() == Node.ELEMENT_NODE){ // This prints the node id if(getTagValue(element)!=""){ System.out.println(getTagValue(element)+" = I am the node id number!"); } // This prints the name else{ System.out.println(element.getTagName()+" = I am the name!"); } } }}\[/code\]What I'd like to do is somehow compare the elements under each node to a "control" list that contains all of the names, and if it doesn't contain a name, it returns the name and its parent node.In reality, the XML I'm dealing with is much larger, so performance matters, but the concept is the same. Any help would be fantastic.
 
Back
Top