So I’ve managed to parse my XML document successfully using SAX, and I am now trying to parse the XML document using DOM (resulting in the same output as achieved using SAX).
I’ve managed to edit most of the SAX code to work in DOM.
For example with SAX in start and end element I used:
if (qName.equals("Name")){...}
and with DOM in case Node.ELEMENT_NODE I’ve just edited this to:
String name = node.getNodeName();
if (name.equals("Name")){...}
I part I’m stuck with is how I can parse all the endElement in SAX in a similar manner using DOM?
I’ve managed to print all the text using:
case Node.TEXT_NODE:
System.out.print(node.getNodeValue().trim());
but how would I go about printing something after a specific text node? In SAX this could be easily done in endElement.
I’ve been trying to use this to print the text from inside the name elements but it isn’t working.
case Node.TEXT_NODE:
if (node.getNodeName().equals("name")) {
System.out.print(node.getNodeValue().trim());
}
Thanks for any help on this.
Have a look into
Nodejavadoc – it has a fixed name"#text"; you need to check the name of the parent, which is the tag: