0

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();
}
Feedforward
  • 4,521
  • 4
  • 22
  • 34
Zehke
  • 129
  • 1
  • 1
  • 11

2 Answers2

0

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.

Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • Thanks for the answer and the suggestion, its good to know that the code so far is correct. – Zehke Sep 30 '18 at 13:57
0

This Code solved the issue:

    //...
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(url.openStream());
    doc.getDocumentElement().normalize();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");


    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);

    String xmlString = result.getWriter().toString();
    System.out.println(xmlString);
Zehke
  • 129
  • 1
  • 1
  • 11