gpt4 book ai didi

java - JOOX 命名空间支持

转载 作者:行者123 更新时间:2023-12-02 07:43:17 25 4
gpt4 key购买 nike

我正在尝试对某些包含命名空间的 XML 运行一些 xpath 查询。不幸的是,它给了我一个相当可怕的错误消息,因为它不理解 xpath 查询或 XML 中的 namespace 。

示例 xpath 查询为://b:menuBarItem[@label='Risk']/@id

我可以在 JOOX (1.1.0) 中配置命名空间吗? ,与我在这里使用内置 java 库完成的方式类似吗?

  private static NamespaceContext createXPathNameSpaceContext( final Map<String, String> namespaces ) {
NamespaceContext nsContext = new NamespaceContext() {
public Iterator<String> getPrefixes( String namespaceURI ) {
return null;
}
public String getPrefix( String namespaceURI ) {
return null;
}
public String getNamespaceURI( String prefix ) {
if ( namespaces.containsKey( prefix ) ) {
return namespaces.get( prefix );
} else {
return XMLConstants.NULL_NS_URI;
}
}
};
return nsContext;
}

我使用以下代码调用 JOOX。

import static org.joox.JOOX.$;

private void extractResponseData( Document responseXML, String xpath ) {
String match = $( responseXML ).xpath( xpath ).get( 0 ).getNodeValue();
// .. etc..
}

最明显的事情是我还没有注册 namespace ,但我不知道如何注册。我怀疑我目光短浅,忽略了 API 中的某些内容。我从 JOOX 获得的堆栈跟踪如下。

java.lang.RuntimeException: javax.xml.xpath.XPathExpressionException
at org.joox.Impl.xpath(Impl.java:494)
at org.joox.Impl.xpath(Impl.java:467)
at org.joox.Impl.xpath(Impl.java:93)
at end2end.http.HttpAction$XML.extractResponseData(HttpAction.java:181)
at end2end.http.XMLPostAction.extractResponseData(XMLPostAction.java:26)
at end2end.http.HttpAction.perform(HttpAction.java:58)
at end2end.TestXML$UserAction.fire(TestXML.java:176)
at end2end.TestXML$FireAction.fire(TestXML.java:148)
at end2end.TestXML.multipleUsers(TestXML.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:702)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:336)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:65)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:88)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1122)
at org.testng.TestNG.run(TestNG.java:1030)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:215)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: javax.xml.xpath.XPathExpressionException
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(XPathImpl.java:402)
at org.joox.Impl.xpath(Impl.java:486)
at org.joox.Impl.xpath(Impl.java:467)
at org.joox.Impl.xpath(Impl.java:93)
at end2end.http.HttpAction$XML.extractResponseData(HttpAction.java:181)
at end2end.http.XMLPostAction.extractResponseData(XMLPostAction.java:27)
at end2end.http.HttpAction.perform(HttpAction.java:58)
at end2end.TestXML$UserAction.fire(TestXML.java:176)
at end2end.TestXML$FireAction.fire(TestXML.java:148)
at end2end.TestXML.multipleUsers(TestXML.java:120)
... 24 more
Caused by: com.sun.org.apache.xpath.internal.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace:
at com.sun.org.apache.xpath.internal.compiler.XPathParser.errorForDOM3(XPathParser.java:653)
at com.sun.org.apache.xpath.internal.compiler.Lexer.mapNSTokens(Lexer.java:638)
at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:265)
at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:96)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(XPathParser.java:110)
at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:176)
at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:264)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(XPathImpl.java:394)
at org.joox.Impl.xpath(Impl.java:486)
at org.joox.Impl.xpath(Impl.java:467)
at org.joox.Impl.xpath(Impl.java:93)
at end2end.http.HttpAction$XML.extractResponseData(HttpAction.java:181)
at end2end.http.XMLPostAction.extractResponseData(XMLPostAction.java:26)
at end2end.http.HttpAction.perform(HttpAction.java:58)
at end2end.TestXML$UserAction.fire(TestXML.java:176)
at end2end.TestXML$FireAction.fire(TestXML.java:148)
at end2end.TestXML.multipleUsers(TestXML.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:702)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1197)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1122)
at org.testng.TestNG.run(TestNG.java:1030)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
... 1 more

最佳答案

jOOX 尚不支持 XML 命名空间。这里有一个关于此主题的待处理功能请求:

http://code.google.com/p/joox/issues/detail?id=9

关于java - JOOX 命名空间支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11297311/

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