Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a bug in getNodeName() of the xml parser?

public static File datafile = new File("myxmlfile.xml");
// ...

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
    DocumentBuilder db = dbf.newDocumentBuilder();
    doc = db.parse(datafile);
} catch(Exception e) { }
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("ZAdata");
Node no = doc.getElementsByTagName("ZAdata").item(recNo);  // ZAdata is the name of the root, recNo is my current record no
getField(no, "DATUM");  // try to get the value of the node "DATUM"
// ...


static String getField(Node no, String feld) {
    // try to get the value of the node "feld"

    String ret = "*n/f*";  // not found
    NodeList ndl = no.getChildNodes();
    Node nd;
    for (int i = 0; i < ndl.getLength(); i++) {
        nd = ndl.item(i);
        if (nd.getNodeType() == Node.ELEMENT_NODE) {
            String a = nd.getNodeName().toString().trim().toLowerCase();
            String b = feld.toString().trim().toLowerCase();
            if (a.indexOf(b) >= 0) {      // == does not work!
                ret = nd.getTextContent().toString();
            }
        }
    }
    return ret;
}


The last if (... does not work with ==. That means,  if (a == b)  would never get true, even if a and b are apparently identical. I also checked the types of a and b and both are String. Also the length() of both is identical. So what's the difference? And why does  if (a.indexOf(b) >= 0)  work? Also the return value  ret = nd.getTextContent().toString();  has the same problem.
Or am I completely wrong and there is a simpler way to get the value of a node by the name of the node?
You can try it with every xml file.

What I have tried:

some tests to solve the problem
Posted
Updated 15-Dec-22 4:18am
Comments
Richard MacCutchan 15-Dec-22 10:17am    
I just tried a simple test of indexOf and it works correctly. You need to show the exact content of the two strings and the result of the test.

See Richard Deeming's answer.
Richard Deeming 15-Dec-22 10:19am    
Based on the description, the indexOf works; it's the == that doesn't. :)
Richard MacCutchan 15-Dec-22 10:23am    
:blush:

1 solution

Ah, the joy of Java strings!

The equality operator compares the references, not the content of the strings. To compare two strings for equality, you need to use the equals method.
Java
if (a.equals(b)) {

NB: Rather than converting both strings to lower-case, you could use the equalsIgnoreCase method[^]:
Java
String a = nd.getNodeName().toString().trim();
String b = feld.toString().trim();
if (a.equalsIgnoreCase(b)) {
However, XML is designed to be case-sensitive, so this could be a bug in your code.
 
Share this answer
 
Comments
heinkurz 15-Dec-22 13:13pm    
aaaah ...
Thanks so much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900