错误 XPATH KML JDOM
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您看到的错误看起来像是 web 应用程序部署问题。如果您发布完整的堆栈跟踪以及原因,我可能可以提供进一步的帮助。您是否已将所有必需的 jar 包含到项目中?
然而,还有一些其他问题需要修复,否则代码将无法按预期工作。
首先,KML 文件无效。结束标记
与开始标记
不匹配。 XML 区分大小写。其次,您使用的 XPath 不支持命名空间。假设没有前缀的标签位于默认命名空间中。您需要添加此默认命名空间。
我制作了一个小演示,加载并解析以下 KML(已更正)文件(另存为 test.kml)
演示类(另存为名为 ReadKml.java 的文件)并放在与 test.kml 相同的目录中)
该演示需要 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)
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 ReadKml
This results in the System.output of
LookAt
, which is simply theElement.name()
.I hope this helps.