开发者

Error XPATH KML JDOM

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"开发者_JAVA技巧 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)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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 demo class (save as a file called ReadKml.java and put in the same directory as test.kml)

import java.io.*;
import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.xpath.XPath;

public class ReadKml {
    public static void main(String args[]) throws Exception {
        File kmlFile = new File("test.kml");
        SAXBuilder builder = new SAXBuilder();
        Document document = builder.build(kmlFile);

        XPath xPath = XPath.newInstance("//k:kml/k:Document/k:Placemark/k:LookAt");
        xPath.addNamespace("k", document.getRootElement().getNamespaceURI());

        Element node = (Element) xPath.selectSingleNode(document.getRootElement());
        System.out.println(node.getName());
    }
}

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 example java -cp .;jars\jdom.jar;jars\jaxen.jar ReadKml

This results in the System.output of LookAt, which is simply the Element.name().

I hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜