开发者

XmlDocment - how to get nodes by filtering using XPath

If you ha开发者_JS百科ve an XML document and you need to find certain nodes based on certain attribute values (4 in number) which one would be the correct approach (in terms of performance):-

a) Filter the XML document (with XPath) to get node list which match any of the attribute values and then traverse through the filtered Node list to get nodes having a particular attribute value using If-else.

b) Filter the XML document (with XPath) for each attribute value separately.

<Nodes>
  <a class="myclass" type="type1">some text</a>
  <a class="myclass" type="type2">some text</a>
  <img src = "myGraphic.jpg?id={Guid}"/>
</Nodes>

I am using the below XPath (which might be incorrect :-))

"//A[@class] | //a[@class] | //IMG[@src] | //img[@src]"

The goal is to get the separate list of all a having type="type1" a separate list of type="type2" and a separate list of ids in the img tag.


My rough answer would be, performance is not going to really matter much unless you have a very large document or set of documents.

In that case, you probably will want to use SAX, and in any case, you'll want to traverse the document(s) only once, and not hold the whole thing in memory. So you'll be streaming through the documents, stopping at each a element, and appending it to list1 or list2 depending on its type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜