Sunday, 8 February 2015

JDom not recognising all of XML - "No Doctype Declaration"



I am downloading data from Open Street maps and attempting to store it in an XML file.


The xml file is returned in the following format:



<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.3.3 (21759 thorn-01.openstreetmap.org)"
copyright="OpenStreetMap and contributors" attribution="http://ift.tt/ItuEqj"
license="http://ift.tt/1oPTky1">
<bounds minlat="52.4440800" minlon="-1.9305700" maxlat="52.4451000"
maxlon="-1.9280700" />
<node id="1" visible="true" version="1" changeset="21443971"
timestamp="2014-04-01T17:54:04Z" user="brianboru" uid="9065" lat="52.4424918"
lon="-1.9290400" />
<node id="2" visible="true" version="1" changeset="21443971"
timestamp="2014-04-01T17:54:04Z" user="brianboru" uid="9065" lat="52.4424918"
lon="-1.9290400" />
<node id="3" visible="true" version="1" changeset="21443971"
timestamp="2014-04-01T17:54:04Z" user="brianboru" uid="9065" lat="52.4424918"
lon="-1.9290400" />
<node id="4" visible="true" version="1" changeset="21443971"
timestamp="2014-04-01T17:54:04Z" user="brianboru" uid="9065" lat="52.4424918"
lon="-1.9290400" />
<node id="5" visible="true" version="1" changeset="21443971"
timestamp="2014-04-01T17:54:04Z" user="brianboru" uid="9065" lat="52.4424918"
lon="-1.9290400" />
<node id="6" visible="true" version="1" changeset="21443971"
timestamp="2014-04-01T17:54:04Z" user="brianboru" uid="9065" lat="52.4424918"
lon="-1.9290400" />
<node id="7" visible="true" version="1" changeset="21443971"
timestamp="2014-04-01T17:54:04Z" user="brianboru" uid="9065" lat="52.4424918"
lon="-1.9290400" />
<node id="8" visible="true" version="1" changeset="21443971"
timestamp="2014-04-01T17:54:04Z" user="brianboru" uid="9065" lat="52.4424918"
lon="-1.9290400" />
<node id="" visible="true" version="1" changeset="21443971"
timestamp="2014-04-01T17:54:04Z" user="brianboru" uid="9065" lat="52.4424918"
lon="-1.9290410" />

<way id="5" visible="true" version="1" changeset="21222698"
timestamp="2014-03-21T07:25:03Z" user="brianboru" uid="9065">
<nd ref="1" />
<nd ref="2" />
<nd ref="4" />


<tag k="addr:city" v="Birmingham" />
<tag k="addr:housenumber" v="97" />
<tag k="addr:postcode" v="B29 7DX" />
<tag k="addr:street" v="Alton Road" />
<tag k="building" v="terraced_house" />
<tag k="source" v="bing" />
</way>

<way id="6" visible="true" version="1" changeset="21222698"
timestamp="2014-03-21T07:25:03Z" user="brianboru" uid="9065">
<nd ref="1" />
<nd ref="2" />
<nd ref="3" />
<nd ref="6" />
<nd ref="8" />


<tag k="addr:city" v="Birmingham" />
<tag k="addr:housenumber" v="97" />
<tag k="addr:postcode" v="B29 7DX" />
<tag k="addr:street" v="Alton Road" />
<tag k="building" v="terraced_house" />
<tag k="source" v="bing" />
</way>

<way id="7" visible="true" version="1" changeset="21222698"
timestamp="2014-03-21T07:25:03Z" user="brianboru" uid="9065">
<nd ref="1" />
<nd ref="2" />
<nd ref="4" />
<nd ref="6" />
<nd ref="8" />


<tag k="addr:city" v="Birmingham" />
<tag k="addr:housenumber" v="97" />
<tag k="addr:postcode" v="B29 7DX" />
<tag k="addr:street" v="Alton Road" />
<tag k="building" v="terraced_house" />
<tag k="source" v="bing" />
</way>

<way id="8" visible="true" version="1" changeset="21222698"
timestamp="2014-03-21T07:25:03Z" user="brianboru" uid="9065">
<nd ref="1" />
<nd ref="2" />
<nd ref="5" />
<nd ref="7" />


<tag k="addr:city" v="Birmingham" />
<tag k="addr:housenumber" v="97" />
<tag k="addr:postcode" v="B29 7DX" />
<tag k="addr:street" v="Alton Road" />
<tag k="building" v="terraced_house" />
<tag k="source" v="bing" />
</way>




</osm>


I have tested and can confirm the data is retrieved in this format as if I download it and write it line by line to file. However, I do not want to write to file and instead want to pass the XML document around my Java code. This is why I began using JDom.


I have the following code for using JDom to download this data and store



URL url = new URL("http://ift.tt/1ftwOtA");
URLConnection conn = url.openConnection();

conn.setDoOutput(true);

OutputStreamWriter writer = new OutputStreamWriter(
conn.getOutputStream());

writer.write(urlParameters);
writer.flush();
System.out.println("finshed downloading everything");

BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
System.out.println("read it all in ");

String line;

while ((line = reader.readLine()) != null) {
responseBuilder.append(line + '\n');
}

System.out.println("finsined appending response");
SAXBuilder sb = new SAXBuilder();
System.out.println("Made sax ");

Document d = null;
try {
d = sb.build(new StringReader(responseBuilder.toString()));
System.out.println("Built doc");

} catch (Exception e) {
System.out.println(e);
}
writer.close();
reader.close();


}


When I call d.toString() I recieve the following error in my console:



[Document: No DOCTYPE declaration, Root is [Element: <osm/>]]


If I call d.getContentSize() it returns 1.


Can anyone help me please? how do I get it to recognise the rest of the elements except the closing tag?


I am guessing this is because JDom perhaps needs a DTD to work? I wrote a osm.dtd and saved it in my project but am unsure how to load it. Any help or advice would be appreciated.


No comments:

Post a Comment