在XPath中处理元素的最有效方法是什么?

时间:2022-09-23 17:13:59

I have a Java program which cares about efficiency. There I use XPaths.

我有一个关心效率的Java程序。我使用xpath。

In XPath I can select elements starting from root

在XPath中,我可以从root开始选择元素

/root/a/b/c/d/e

or use the descendent-or-self axis:

或者使用后代-自我轴:

//e

What will be most efficient method among these two?

这两种方法中最有效的方法是什么?

3 个解决方案

#1


3  

A direct path will tend to perform better than one using the more general descendant-or-self (//) axis, however:

然而,使用更一般的后代-或-自我(/)轴,直接路径的性能往往比直接路径更好:

  • Implementations could vary (but as a general rule, direct paths perform better).
  • 实现可能会有所不同(但作为一般规则,直接路径执行得更好)。
  • The difference can be minor enough not to matter, especially for small documents.
  • 差异可能很小,不重要,特别是对于小文档。
  • As with all performance concerns, measure before optimizing to avoid expending effort in areas other than true bottlenecks.
  • 与所有性能问题一样,在优化之前进行度量,以避免在真正的瓶颈之外的领域花费精力。

#2


1  

I would imagine that /root/a/b/c/d/e would be more efficient, because in the first case, the XPath processor can eliminate a lot of branches, whereas in the second case (//e) the XPath processor has to search the entire document tree.

我认为/root/a/b/c/d/e会更有效,因为在第一种情况下,XPath处理器可以消除很多分支,而在第二种情况下(//e), XPath处理器必须搜索整个文档树。

You should write a small Java program that excersizes the two different ways, and then see how long it takes to run 1000 loops.

您应该编写一个小的Java程序,从中摘录两种不同的方法,然后查看运行1000个循环需要多长时间。

#3


1  

Understanding the leading / and // constructs is very important.

理解领导/和//构念是非常重要的。

A leading / starts a path that is always relevant to the root node. Therefore, even though we are searching a sub-node, the XPath:

引导/启动始终与根节点相关的路径。因此,即使我们正在搜索一个子节点,XPath:

root/a/b/c

... will still return every c node in your XML document even though they are not descendants of the first c node. Likewise, the XPath:

…将仍然返回XML文档中的每个c节点,即使它们不是第一个c节点的后代。同样,XPath:

//e/

... will still return every e node in your XML document, not just the descendants of your first c node.

…将仍然返回XML文档中的每个e节点,而不仅仅是第一个c节点的后代。

#1


3  

A direct path will tend to perform better than one using the more general descendant-or-self (//) axis, however:

然而,使用更一般的后代-或-自我(/)轴,直接路径的性能往往比直接路径更好:

  • Implementations could vary (but as a general rule, direct paths perform better).
  • 实现可能会有所不同(但作为一般规则,直接路径执行得更好)。
  • The difference can be minor enough not to matter, especially for small documents.
  • 差异可能很小,不重要,特别是对于小文档。
  • As with all performance concerns, measure before optimizing to avoid expending effort in areas other than true bottlenecks.
  • 与所有性能问题一样,在优化之前进行度量,以避免在真正的瓶颈之外的领域花费精力。

#2


1  

I would imagine that /root/a/b/c/d/e would be more efficient, because in the first case, the XPath processor can eliminate a lot of branches, whereas in the second case (//e) the XPath processor has to search the entire document tree.

我认为/root/a/b/c/d/e会更有效,因为在第一种情况下,XPath处理器可以消除很多分支,而在第二种情况下(//e), XPath处理器必须搜索整个文档树。

You should write a small Java program that excersizes the two different ways, and then see how long it takes to run 1000 loops.

您应该编写一个小的Java程序,从中摘录两种不同的方法,然后查看运行1000个循环需要多长时间。

#3


1  

Understanding the leading / and // constructs is very important.

理解领导/和//构念是非常重要的。

A leading / starts a path that is always relevant to the root node. Therefore, even though we are searching a sub-node, the XPath:

引导/启动始终与根节点相关的路径。因此,即使我们正在搜索一个子节点,XPath:

root/a/b/c

... will still return every c node in your XML document even though they are not descendants of the first c node. Likewise, the XPath:

…将仍然返回XML文档中的每个c节点,即使它们不是第一个c节点的后代。同样,XPath:

//e/

... will still return every e node in your XML document, not just the descendants of your first c node.

…将仍然返回XML文档中的每个e节点,而不仅仅是第一个c节点的后代。