Search code examples
javajdom

How do i fix Document: null error on GML-Response


I have to convert a GML-Server Response to a GPX-File with jdom in Java So far the Get-Feature-Request i send to the server is correct and gives me a GML-File as response, but when i want to print the file it says [#document: null]

The output on console:

url https://www.geoportal-amt-beetzsee.de/isk/beet_radwanderwege?service=WFS&version=1.0.0&REQUEST=GetFeature&typename=Riewend-Burgwall-Wanderweg

[#document: null]

try {
    //zu Funktionstestzwecken - löschen wenn nicht mehr benötigt
    String typename = gui.ConverterDialog.tfconverter.getText();
    String urlString = gui.UrlDialog.txturlinput.getText() + "?service=WFS&version=1.0.0&REQUEST=GetFeature&typename=" + typename;

    //entfernen wenn nicht mehr benötigt
    System.out.println("url "+ urlString);
    URL url = new URL(urlString);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(url.openStream());
    //doc.getDocumentElement().normalize();

    //entfernen wenn nicht mehr benötigt
    //doc null ???
    System.out.println(doc);

} catch(Exception e) {
    String errorMessage = "An error occured:" + e;
    System.err.println(errorMessage);
    e.printStackTrace();
}

Solution

  • When the line

    System.out.println(doc);
    

    runs, it calls the toString() method of the document doc and prints out that. The toString() method of the document doesn't serialize the XML document to a string; instead it just prints the node name and its value. The name of the document node is #document, and document nodes don't have values, so null gets printed instead.

    I tried running your XML parsing code on a test XML document and it also printed out [#document: null].

    It might look like your XML parsing has failed and left you with an empty document, but I don't believe that to be the case. Your code is quite probably working correctly.

    If you want to serialize an XML document to a string, see this question.