gpt4 book ai didi

java - 错误 XPATH KML JDOM

转载 作者:行者123 更新时间:2023-11-30 09:48:28 25 4
gpt4 key购买 nike

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

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<

我的代码:

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>

最佳答案

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

但是,还有一些其他问题需要解决,否则代码将无法按预期工作。

首先,KML 文件无效。结束标记 </Kml>与开始标签 <kml> 不匹配. XML 区分大小写。

其次,您使用的 XPath 不是 namespace意识到的。假定没有前缀的标记位于默认 namespace 中。您需要添加此默认命名空间。

我制作了一个加载和解析以下 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());
}
}

demo要求JDOM在classpath下进行编译,例如在Windows上编译demo类型javac -cp jars\jdom.jar ReadKml.java .运行演示也需要 Jaxen,因此将其添加到类路径中,例如 java -cp .;jars\jdom.jar;jars\jaxen.jar ReadKml

这导致 System.output 为 LookAt ,这就是 Element.name() .

希望对您有所帮助。

关于java - 错误 XPATH KML JDOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6233070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com