5

enter image description hereI have tried all possible answers on Stackoverflow but I can't get this resolved.

I have the following xml as a String:

private static final String xmlStr = "<parameters>\n" +
            "  <parameter>\n" +
            "    <name>emp</name>\n" +
            "    <keyvalue>John Smith</keyvalue>\n" +
            "  </parameter>\n" +
            "  <parameter>\n" +
            "    <name>age</name>\n" +
            "    <keyvalue>22</keyvalue>\n" +
            "  </parameter>\n" +
            "  <parameter>\n" +
            "    <name>Birth Date</name>\n" +
            "    <keyvalue>02/05/1978</keyvalue>\n" +
            "  </parameter>\n" +
            "  <parameter>\n" +
            "    <name>password</name>\n" +
            "    <keyvalue>ye63633</keyvalue>\n" +
            "  </parameter>\n" +
            "</parameters>";

The parameters can be replaced with any string (which I tried without sucess), and the following code returns null document:

 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(xmlStr.getBytes("UTF-8")));

I have tried replacing document with:

Document document = builder.parse(new InputSource(new StringReader(xmlStr)));//This used to work 2 weeks ago.

document is returning null document all the time and I can't figure out what I am doing wrong. The same code used to work few weeks ago.

I am using Java 1.8 !!

I appericiate your help.

UPDATE:

Here is the full code that takes the above xmlStr and try to access age of the employee.

private String getElementValue(String tagName, String xmlString) {

        try {

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new InputSource(new StringReader(xmlString)));
            Element rootElement = document.getDocumentElement();

            NodeList list = rootElement.getElementsByTagName(tagName);

            if (list != null && list.getLength() > 0) {
                NodeList subList = list.item(0).getChildNodes();

                if (subList != null && subList.getLength() > 0) {
                    return subList.item(0).getNodeValue();
                }
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

Based on the comments, the document is not null at all but there is an issue accessing the desired xml tag. So the code NodeList list = rootElement.getElementsByTagName("age"); has size 0. i.e its not entering the loop at all.

Community
  • 1
  • 1
WowBow
  • 7,137
  • 17
  • 65
  • 103
  • What *exactly* do you mean by "null document"? Are you just relying on the result of `document.toString()`? – Jon Skeet Jan 19 '17 at 21:57
  • I will post the exact error in a second. – WowBow Jan 19 '17 at 21:58
  • If you could rewrite the question as a [mcve] that would really help... – Jon Skeet Jan 19 '17 at 21:59
  • @JonSkeet I appericiate that. I forgot to put the exact error. I put a screenshot now. Let me know if it still needs more information. Thanks! – WowBow Jan 19 '17 at 22:01
  • I'll go further and say that we absolutely need a small runnable program which illustrates the problem; it appears that wouldn't be that much trouble for this question. The image you have pasted in is from a tool with which I am not familiar, which appears to say that "#document" is null but proceeds to list values of lots of fields in it. – arcy Jan 19 '17 at 22:02
  • 1
    I don't see any error. I see a `toString()` result that contains the text "null", but that's definitely not an error. Have you tried actually *using* the document? Note that the debugger is showing 39 nodes... – Jon Skeet Jan 19 '17 at 22:04
  • Let me re run debugger again and put more info. Few more minutes. – WowBow Jan 19 '17 at 22:05
  • You guys are right. The document is not null at all. It's just accessing the tag name is not working as desired. I am still experimenting but I have updated the sample code base. – WowBow Jan 19 '17 at 22:45
  • Never mind guys. Sorry for wasting your time. I was accessing the tags in the wrong way. – WowBow Jan 19 '17 at 23:08

1 Answers1

7

It seems to be working correctly. The following code, added after builder.parse, shows that the XML is being parsed:

System.out.println("Root element: " + document.getDocumentElement().getNodeName());
NodeList nodeList = document.getElementsByTagName("parameter");
for (int temp = 0; temp < nodeList.getLength(); temp++) {
    org.w3c.dom.Node node = nodeList.item(temp);
    System.out.println("\nCurrent element: " + node.getNodeName());
    if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
        Element element = (Element) node;
        System.out.println("Name: " + element.getElementsByTagName("name").item(0).getTextContent());
        System.out.println("Key Value: " + element.getElementsByTagName("keyvalue").item(0).getTextContent());
    }
}

Output:

Root element: parameters

Current element: parameter
Name: emp
Key Value: John Smith

Current element: parameter
Name: age
Key Value: 22

Current element: parameter
Name: Birth Date
Key Value: 02/05/1978

Current element: parameter
Name: password
Key Value: ye63633
Loris Securo
  • 7,538
  • 2
  • 17
  • 28