I am using dom4j library(SAXReader) in java and want to know how can I read and fetch all the values of an XML attribute present in the child XML tags of some parent XML tags. for eg.
....
<ParentTag1>
<ChildTag1 SomeAttributeName1 = "val1">....</ChildTag1>
<ChildTag2>...</ChildTag2>
<ChildTag3 SomeAttributeName1 = "val2">...</ChildTag3>
</ParentTag1>
....
<ParentTag2>
<ChildTag1 SomeAttributeName2 = "val1">....</ChildTag1>
<ChildTag2>...</ChildTag2>
<ChildTag3 SomeAttributeName2 = "val2">...</ChildTag3>
</ParentTag2>
......
Let's say the attribute is SomeAttributeName1
Questions:
- How can I collect all the values of
SomeAttributeName1to a list or set ? eg. value set/list = {'val1', 'val2'}- While collecting the values for
SomeAttributeName1, I want to make sure that I only look into child tags of a particular parent tag(here, only look into child tags of<ParentTag1>) and not the complete XML, is it possible to do so ? If yes, please explain your answer using the same ? - Following above question, which is faster/efficient and why - reading child elements of
<ParentTag1>OR reading complete XML to look for the values ofSomeAttributeName1?
- While collecting the values for
Thanks
Since DOM4J has built-in XPATH support, using that is probably the best apprach:
The XPATH expression selects all Attribute nodes in all elements that have an ancestor element named ParentTag1.