优化 DOM 和 XPath Java 代码

发布于 2024-12-18 08:47:30 字数 982 浏览 7 评论 0原文

我的代码太慢,但我不知道如何改进它。从磁盘读取 1k 文件到 DOM 大约需要 20 毫秒,这可能还不错,具体取决于磁盘,但是我还有另外 20 毫秒来处理 xpath 语句,这太多了。这是一些带有时间注释的示例代码。我该如何改进代码?

这发生在构造时:

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = this.dbFactory.newDocumentBuilder(); 
XPathExpression[] ex = new XPathExpression[]{about 30 different expressions}
XPathExpression mainEx =xPath.compile("/rootElement/firstLevel/secondLevel");

然后代码:

Document doc = this.dBuilder.parse("somefile.xml");
//took 20 ms until here
NodeList nodes = (NodeList) mainEx .evaluate,doc, XPathConstants.NODESET);
 //took another 20 ms until here !!!
    for (int i = 0; i < nodes.getLength(); i++) {
    Node n = nodes.item(i);
    for (XPathExpression e:ex) {
         String v = (String) e.evaluate(n, XPathConstants.STRING);
        if (v != null) {
            System.out.println(v);
        }
    }
    }
    //this only takes 5 ms

My code is too slow, but I'm not sure how to improve it. Reading from disk into DOM for a 1k-file takes about 20 ms, that might be okay depending on the disk, but then I've got another 20 ms for working on a xpath statement, which is far too much. Here is some sample code with time comments. How can I improve the code?

This happens at construction time:

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = this.dbFactory.newDocumentBuilder(); 
XPathExpression[] ex = new XPathExpression[]{about 30 different expressions}
XPathExpression mainEx =xPath.compile("/rootElement/firstLevel/secondLevel");

Then the code:

Document doc = this.dBuilder.parse("somefile.xml");
//took 20 ms until here
NodeList nodes = (NodeList) mainEx .evaluate,doc, XPathConstants.NODESET);
 //took another 20 ms until here !!!
    for (int i = 0; i < nodes.getLength(); i++) {
    Node n = nodes.item(i);
    for (XPathExpression e:ex) {
         String v = (String) e.evaluate(n, XPathConstants.STRING);
        if (v != null) {
            System.out.println(v);
        }
    }
    }
    //this only takes 5 ms

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

原谅过去的我 2024-12-25 08:47:30

您可能遇到了我在这里记录的这个问题:

Java XPath(Apache JAXP实现)性能

本质上,您应该添加这些 JVM 参数来大幅加速 Xalan 的 XPath 实现:

-Dorg.apache.xml.dtm.DTMManager=
  org.apache.xml.dtm.ref.DTMManagerDefault

或者

-Dcom.sun.org.apache.xml.internal.dtm.DTMManager=
  com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault

You're probably suffering from this problem that I documented here:

Java XPath (Apache JAXP implementation) performance

Essentially, you should add these JVM arguments to heavily speed up Xalan's XPath implementation:

-Dorg.apache.xml.dtm.DTMManager=
  org.apache.xml.dtm.ref.DTMManagerDefault

or

-Dcom.sun.org.apache.xml.internal.dtm.DTMManager=
  com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault
岁月染过的梦 2024-12-25 08:47:30

您应该将 XPath 表达式预编译为 XPathExpression,使用 XPath.compile。然后调用 XPathExpression.evaluate

如果您多次执行它,这将节省您的时间。我假设情况确实如此,或者 20 毫秒应该不重要。

编辑:正如评论中提到的,这个 问题 有更多信息,包括JVM 参数。

You should pre-compile your XPath expression to a XPathExpression, using XPath.compile. Then call XPathExpression.evaluate.

This will save you time if you're executing it more than once. I'm assuming this is the case, or 20 ms shouldn't matter.

EDIT: As mentioned in the comments, this question has further information, including a JVM parameter.

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