2

I'm trying to parse a string that contains an XML, into a Document Object in Java. Here's the source code of the method:

private Document getDocument(String xml) {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
    DocumentBuilder builder = null;  

    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    Document doc = null;
    try {
        doc = builder.parse(new InputSource(new StringReader(xml)));
    } catch (SAXException e) {
        System.out.println(e.toString());
    } catch (IOException e) {
        System.out.println(e.toString());
    }

    return doc;
}

What I always get as a value of the doc variable according to the debugger is

[#document: null]

Does anyone have a clue what is wrong?

Thank you for your time!

Dimitris P.
  • 373
  • 2
  • 3
  • 14
  • Possible duplicate of [How to parse a String containing XML in Java and retrieve the value of the root node?](http://stackoverflow.com/questions/8408504/how-to-parse-a-string-containing-xml-in-java-and-retrieve-the-value-of-the-root) – DimaSan Aug 30 '16 at 12:45

2 Answers2

2

This is normal as the debugger is invoking Document#toString() which prints [node_name: node_value]. The node value of a document node is null according to the W3C spec. If you want to print the name of the root element, you should evaluate doc.getDocumentElement().getNodeName().

M A
  • 71,713
  • 13
  • 134
  • 174
1

It would be nice if you post the XML document as well to see the structure, but this is what I do to read an XML.

    // Opening and creating file Objects
    File inputFile = new File(xmlFile);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(inputFile);
    doc.getDocumentElement().normalize();

After that, you can use the doc.getElementsByTagName("Root Node"); to get the root node of your XML. The object has a lot of properties, like getFirstChild() or getChildNodes() depending on the strucure, Ill suggest to read this:

http://www.w3schools.com/xml/dom_intro.asp

Jose Bernhardt
  • 887
  • 1
  • 10
  • 24