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.

9
  • What exactly do you mean by "null document"? Are you just relying on the result of document.toString()? Commented Jan 19, 2017 at 21:57
  • I will post the exact error in a second. Commented Jan 19, 2017 at 21:58
  • If you could rewrite the question as a minimal reproducible example that would really help... Commented Jan 19, 2017 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! Commented Jan 19, 2017 at 22:01
  • 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... Commented Jan 19, 2017 at 22:04

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.