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

[英]What is the most efficient way of addressing an element in XPath?


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節點的后代。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/04/e23e90199c76787cd0d99eb0c9961bf5.html



 
  © 2014-2022 ITdaan.com 联系我们: