I am trying to parse a XML document using Xerces, but I cant seem to access the data within the elements, below is a sample XML document;
<sample>
<block>
<name>tom</name>
<age>44</age>
<car>BMW</car>
</block>
<block>
<name>Jenny</name>
<age>23</age>
<car>Ford</car>
</block>
</sample>
SO far the only output I can produce is;
Sample
block
name
age
car
block
name
age
car
Which is just a list of the node names. I have tried node.getValue(), but this just returns null, so im guessing thats wrong!
How can I access the data inside? Here is what is the basics so far;
public static void display(String file) {
try{
DOMParser parser = new DOMParser();
parser.parse(file);
Document doc = parser.getDocument();
read(doc);
}
catch(Exception e){e.printStackTrace(System.err);}
}
public static void read(Node node) {
if(node == null) {return;}
int type = node.getNodeType();
//System.out.print((node));
switch (type) {
case Node.DOCUMENT_NODE: {
display_all(((Document)node).getDocumentElement());
break;
}
case Node.TEXT_NODE:
break;
case Node.ELEMENT_NODE: {
System.out.println(node.getNodeName());
NodeList child = node.getChildNodes();
if(child != null) {
int length = child.getLength();
for (int i = 0; i < length ; i++) {
display_all(child.item(i));
}
}
break;
}
}
}
getTextValue()? It'd be easier if you'd posted some code.getTextContent()? (mis-typed) But why are you skipping text nodes?