我正在尝试对一些包含名称空间的 XML 运行一些 xpath 查询。不幸的是,它给了我一个相当可怕的错误消息,因为它不理解 xpath 查询或 XML 中的命名空间。
一个示例 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..
}
显而易见的事情是我没有注册命名空间,但我不知道如何注册。我怀疑我是相当短视的,并且忽略了 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