使用 JDom 的 XPath
在下面的代码中,我尝试使用 XPath 访问我的“处理程序”XML 元素,但我没有运气 - “elemHandler”元素始终为 null。有人可以与我分享明显的解决方案吗?提前致谢。
import java.io.IOException;
import java.io.StringReader;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
public class XPathTest {
private static String jobString = "<job name=\"Workflow.JOB\">" +
" <handler name=\"xslt.converter\"/>" +
" <handler name=\"openoffice.renderer\">" +
" <opts input=\"ODS\" output=\"PDF\"/>" +
" </handler>" +
"</job>";
public static void main(String[] args) {
try {
Element elemJobInfo = new SAXBuilder().build(new StringReader(jobString)).detachRootElement();
XPath handlerExpression = XPath.newInstance("//stp:handler[2]");
handlerExpression.addNamespace("stp", "http://service.mine.org/dgs");
Element elemHandler = (Element) handlerExpression.selectSingleNode(elemJobInfo);
jobString = elemHandler.toString();
}
catch (IOException e) {
System.out.println("Failure: " + e);
}
catch (JDOMException e) {
System.out.println("Failure: " + e);
}
catch (Exception e) {
System.out.println("Failure: " + e);
}
}
}
In my code below, I am trying to access my 'handler' XML elements using XPath, but I am having no luck - the 'elemHandler' element is always null. Can anyone share with me the obvious solution? Thanks in advance.
import java.io.IOException;
import java.io.StringReader;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
public class XPathTest {
private static String jobString = "<job name=\"Workflow.JOB\">" +
" <handler name=\"xslt.converter\"/>" +
" <handler name=\"openoffice.renderer\">" +
" <opts input=\"ODS\" output=\"PDF\"/>" +
" </handler>" +
"</job>";
public static void main(String[] args) {
try {
Element elemJobInfo = new SAXBuilder().build(new StringReader(jobString)).detachRootElement();
XPath handlerExpression = XPath.newInstance("//stp:handler[2]");
handlerExpression.addNamespace("stp", "http://service.mine.org/dgs");
Element elemHandler = (Element) handlerExpression.selectSingleNode(elemJobInfo);
jobString = elemHandler.toString();
}
catch (IOException e) {
System.out.println("Failure: " + e);
}
catch (JDOMException e) {
System.out.println("Failure: " + e);
}
catch (Exception e) {
System.out.println("Failure: " + e);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
stp
命名空间怎么了?jobString
中的 XML 不引用任何命名空间。你试过没有前缀吗?What's up with the
stp
namespace? The XML injobString
doesn't reference any namespaces. Have you tried it without the prefix?评估XPath 表达式:
//stp:handler[2]
的 XML 文档没有默认或声明的命名空间,并且所有节点都位于“无命名空间”中。
"http://service.mine.org/dgs"
命名空间中没有任何节点。除非您在实际情况中使用另一个 XML 文档,否则上述表达式不得选择任何节点 —— 这正是您所得到的结果。如果您使用的文档并未显示,但该文档确实具有默认名称空间,那么您很可能在 Java 代码中拼错了名称空间。
另外,请尝试 XPath 表达式的这种变体(带或不带命名空间前缀):
(//stp:handler)[2]
The XML document against which the XPath expression:
//stp:handler[2]
is evaluated, has no default or declared namespaces and all nodes are in "no namespace". There isn't any node in the
"http://service.mine.org/dgs"
namespace. Unless you are using another XML document in your actual case, the above expression must not select any node -- and this is exactly what you get.In case you are using a document that you haven't shown, that really has a default namespace, the chances are you have misspelt the namespace in your Java code.
Also, do try this variation of your XPath expression (with or without the namespace prefix):
(//stp:handler)[2]