Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML and DOM getting #text output

Tags:

java

dom

xml

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 );
    }
}
like image 752
Espen Avatar asked Mar 16 '26 06:03

Espen


1 Answers

"#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.

like image 193
Paul Clapham Avatar answered Mar 17 '26 19:03

Paul Clapham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!