XML and DOM get #text output

I am trying to read a Collada XML file by iterating through a node child list, and each other output reads #text. What is it? My code is:

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 );
    }
}
+3
source share
1 answer

"# text" is simply the result of calling the getNodeName () method in the node text. (As you will see if you look at the API documentation for org.w3c.dom.Node.) If you want the actual textual content of the node, you must use the getNodeValue () method.

And if you did not expect any text nodes, do not forget that even small pieces of spaces, such as newlines, are treated as text.

+4
source

Source: https://habr.com/ru/post/1777109/


All Articles