使用 JDom 的 XPath

发布于 2024-09-11 03:12:24 字数 1498 浏览 2 评论 0原文

在下面的代码中,我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤寂小茶 2024-09-18 03:12:24

stp 命名空间怎么了? jobString 中的 XML 不引用任何命名空间。你试过没有前缀吗?

//handler[2]

What's up with the stp namespace? The XML in jobString doesn't reference any namespaces. Have you tried it without the prefix?

//handler[2]
那些过往 2024-09-18 03:12:24

评估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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文