Home Java DocumentBuilderFactory.parse(); returning null document
Reply: 1

Java DocumentBuilderFactory.parse(); returning null document

display_name Published in 2018-01-13 02:44:58Z

When I call DocumentBuilderFactory.parse(xml-file-path);, it returns a null document. I am 100% sure that the file path for the document is right.My full code is as follows:

public static boolean readXML(String xml) {
    Document dom;
    // Make an instance of the DocumentBuilderFactory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        // use the factory to take an instance of the document builder
        DocumentBuilder db = dbf.newDocumentBuilder();
        // parse using the builder to get the DOM mapping of the
        // XML file
        dom = db.parse(xml);

        System.out.println(dom + " " + xml + " " + dom.getElementById("1"));

        Element doc = dom.getDocumentElement();


        address = getTextValue(address, doc, "address");
        return true;

    } catch (ParserConfigurationException pce) {
    } catch (SAXException se) {
    } catch (IOException ioe) {

    return false;



Preferences.xml is simply:

<address>idk just filler for now</address>

The return I get is:

Why is it returning a null document?

Max Vollmer
Max Vollmer Reply to 2018-01-13 03:29:22Z

It doesn't give you a "null document", it gives you exactly the document you provided. address is your only element and is therefore treated as the document root element. The toString() method of your element object prints element name and element value. Since address is an element node, not a text node, the value is always null (element nodes don't have values, only child nodes). To get the contained text, you either have to get it's direct child, which is a pure text node, or use getTextContent().


will print

[address: null]
[#text: idk just filler for now]
idk just filler for now
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.33044 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO