错误 XPATH KML JDOM

发布于 2024-11-13 11:42:09 字数 1465 浏览 7 评论 0原文

我正在尝试使用 JDOM(XPATH) 读取 KML。该错误不会被异常捕获,仅将鼠标悬停在行 XPath.newInstance("//Kml/Document/Placemark/LookAt"); 处的代码上时捕获。我看到的错误是:

XPath .newInstance("//Kml/Document/Placemark/LookAt"); =>目标虚拟机中发生异常:WEB9031:WebappClassLoader 无法加载资源 [java.lang.ExceptionInInitializerError],因为它尚未启动,或已停止<

我的代码:

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());
    ...
}

示例 KML 文件:

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傻比既视感 2024-11-20 11:42:09

您看到的错误看起来像是 web 应用程序部署问题。如果您发布完整的堆栈跟踪以及原因,我可能可以提供进一步的帮助。您是否已将所有必需的 jar 包含到项目中?

然而,还有一些其他问题需要修复,否则代码将无法按预期工作。

首先,KML 文件无效。结束标记 与开始标记 不匹配。 XML 区分大小写。

其次,您使用的 XPath 不支持命名空间。假设没有前缀的标签位于默认命名空间中。您需要添加此默认命名空间。

我制作了一个小演示,加载并解析以下 KML(已更正)文件(另存为 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>

演示类(另存为名为 ReadKml.java 的文件)并放在与 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());
    }
}

该演示需要 JDOM 位于类路径中进行编译,例如在 Windows 上编译演示类型 javac -cp jars\jdom.jar读取Kml.java。运行演示还需要 Jaxen,因此将其添加到类路径中,例如 java -cp .;jars\jdom.jar;jars\jaxen.jar ReadKml

这会导致 System.output 为 < code>LookAt,即 Element.name()

我希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文