I am trying to read a KML with JDOM(XPATH). The error is not caught by Exceptions, only with mouse over code at line XPath.newInstance(“//Kml/Document/Placemark/LookAt”); The error I am seeing is:
XPath.newInstance("//Kml/Document/Placemark/LookAt"); = >Exception occurred in target VM: WEB9031: WebappClassLoader unable to load resource [java.lang.ExceptionInInitializerError], because it has not yet been started, or was already stopped<
My code:
public void lerKML() throws Exception {
String path = req.getRealPath("/Usuarios/" + pe.getEmail() + "/"+ pe.getTitulo() + "/" + pe.getNomeArquivoKMLZ());
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(new File(path));
XPath xPath = XPath.newInstance("//Kml/Document/Placemark/LookAt");
Element node = (Element) xPath.selectSingleNode(document.getRootElement());
...
}
Example KML file:
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<Placemark>
<name>teste</name>
<LookAt>
<longitude>-47.82056628282606</longitude>
<latitude>-15.78921645504241</latitude>
<altitude>0</altitude>
<heading>0</heading>
<tilt>0</tilt>
<range>668.1932383230591</range>
</LookAt>
</Placemark>
</Document>
</Kml>
The error you are seeing, it looks like a webapp deployment issue. If you post the full stack trace with the Cause I may be able to help further. Have you included all the required jars to the project?
However, there are a couple of other problems that to be fixed otherwise the code will not work as expected.
Firstly, the KML file is not valid. The closing tag
</Kml>does not match the opening tag<kml>. XML is case-sensitive.Secondly, the XPath you are using is not namespace aware. A tag without a prefix is assumed to be in the default namespace. You need to add this default namespace.
I have made a small demo that loads and parses the following KML (corrected) file (save as test.kml)
The demo class (save as a file called ReadKml.java and put in the same directory as test.kml)
The demo requires JDOM to be on the classpath for compilation, for example on Windows to compile the demo type
javac -cp jars\jdom.jar ReadKml.java. Running the demo requires Jaxen as well, so add that to the classpath, for examplejava -cp .;jars\jdom.jar;jars\jaxen.jar ReadKmlThis results in the System.output of
LookAt, which is simply theElement.name().I hope this helps.