For some reason, I have to manually parse a KML file that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
...
<Placemark>
<Point><coordinates>13.38705,52.52715,0</coordinates></Point>
<Name>My name</Name>
<description xmlns="">Hallo World</description>
</Placemark>
</Document>
</kml>
To map it to java, I wrote the following class
@XmlRootElement(name = "kml", namespace = "http://www.opengis.net/kml/2.2")
public class Kml {
Document document;
@XmlElement(name = "Document")
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}
}
Using Jaxb, I got the following parser.
public class JAXBKmlParser {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public Kml klmParser(final String kmlFile) {
Kml kml = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Kml.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(kmlFile);
kml = (Kml) unmarshaller.unmarshal(reader);
} catch (JAXBException e) {
logger.error("JABX Exception corrupted KML", e);
}
return kml;
}
}
My problem is that the xml attribute is namespacenot recognized.
If I change the annotation
@XmlRootElement(name = "kml", namespace = "http://www.opengis.net/kml/2.2")
to
@XmlRootElement(name = "kml")
and removing the namespace from the header of my KML file, then the parsing works without any problems.
My question is how to deal with this problem without removing the namespace.
Note that the description tag also has a namespace.