Reputation: 3727
I'm trying to read a Collada XML file by iterating through a child node list, and every other output reads #text. What's this about? My code:
public void runTest() {
File file = new File( "test.dae" );
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
}
catch( ParserConfigurationException error ) {
System.out.println( "--ParserConfigurationException: " + error.getMessage() ); return;
}
Document document = null;
try {
document = builder.parse( file );
}
catch( IOException error ) {
System.out.println( "--IOException: " + error.getMessage() ); return;
}
catch( SAXException error ) {
System.out.println( "--SAXException: " + error.getMessage() ); return;
}
Node node = document.getDocumentElement();
String node_name = node.getNodeName();
System.out.println( node_name );
NodeList node_list = node.getChildNodes();
for( int iterator = 0; iterator < node_list.getLength(); iterator++ ) {
Node child_node = node_list.item( iterator );
String child_node_name = child_node.getNodeName();
System.out.println( "-- " + child_node_name );
}
}
Upvotes: 3
Views: 7032
Reputation: 56
"#text" is simply the result of calling the getNodeName() method on a Text node. (As you will see if you look at the API documentation for org.w3c.dom.Node.) If you want the actual text contents of the node, you would use the getNodeValue() method.
And if you weren't expecting there to be any Text nodes, don't forget that even little bits of whitespace, like new-line characters, are treated as text.
Upvotes: 4