2

While using getNodeName, it will return actual value with "#text" as prefix. I do not want that prefix. If I remove space and newlines, getNodeName is working fine. I am using DocumentBuilderFactory,DocumentBuilder and Document for parse xml.

My XML file

<test>
    <a>
        file1
    </a>
    <b>
        file2
    </b>
    <c>
        <files>
            <file>
                myfile1
            </file>
        </files>
    </c>
</test>

My java Method

NodeList childNodes = null;
NodeList parentNodes = xml.getNodeList("test");
int node_len = parentNodes.getLength();
for (int i = 0; i < node_len; i++)
{
    childNodes = parentNodes.item(i).getChildNodes();
    int child_len = childNodes.getLength();
    for (int j = 0; j < child_len; j++)
    {
        Node dataNode = childNodes.item(j);
        System.out.println(dataNode.getNodeName());
    }
}

Please help me to clear this issue. Thanks is advance.

4 Answers 4

3

In XML almost everything is a node, and all nodes implement getNodeName() (or similar syntax in each parser). The elements and attributes are nodes and have explicit node names (elementName (in your case "test", "a", "b", "c", "files", "file") or attributeName (you have no attributes)). text() nodes and and comment() nodes do not have individual node names. The parser will normally give them a single common nodeName of #text or #comment so you can see what type they are. (The only other logical alternatives would be null or emptyString or throw Exception all of which would be worse.)

"While using getNodeName, it will return actual value with "#text" as prefix". Are you sure?

Be sure that you are not confusing the name of a node with its value. There are two separate operations: getNodeName() which should return "#text" for ALL text nodes. getValue() which should return "myfile1" (probably with trailing \n). Note that your file contains many whitespace text nodes.

Note that if you getValue() of an element, that is the concatenated strings of all the descendants, including whitespace.

Note also that the string "myfile1" is NOT a child of the elementNode file. The elementNode has a child text() node whose string value is "myfile1".

Sign up to request clarification or add additional context in comments.

Comments

1

In addition to answer given by @peter.murray.rust I want to suggest you to check whether the node is actually Element (that is expected in your case), cast to Element and invoke getTagName():

if(dataNode instanceof Element) {
    String tag = ((Element)dataNode).getTagName();
}

Comments

0

Try to drop text nodes.

    for (int j = 0; j < child_len; j++)
    {
        Node dataNode = childNodes.item(j);
        if (dataNode.getNodeType() == Node.ELEMENT_NODE) {
                System.out.println(dataNode.getNodeName());
        }
    }

The condition dataNode.getNodeType() == Node.ELEMENT_NODE will drop all non-element nodes.

Comments

0
            for (int j = 0; j < child_len; j++)
            {
                Node dataNode = childNodes.item(j);

                    if(dataNode.getAttributes() != null)
                        System.out.println(dataNode.getNodeName());

                   /*or
                   if (dataNode.getNodeType() == Node.ELEMENT_NODE) 
                   {
                       System.out.println(dataNode.getNodeName());
                   }
                   */
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.