2

How could I split the XML tree to a list of XML objects and then I like to user the function getNodeSet but the return value should include the root object "part"?

require(XML)

txt = "<doc>
         <part>
           <name>ABC</name>
           <type>XYZ</type>
           <cost>3.54</cost>
           <status>available</status>
         </part>
         <part>
           <name>ABC</name>
           <type>XYZ</type>
           <cost>3.54</cost>
           <status>available</status>
         </part>
       </doc>"

doc <- xmlTreeParse(txt, useInternalNodes = TRUE)
special_nodes <- getNodeSet(doc, "/*/part//*")
2
  • For the first question I found xpathApply(doc, "//doc/*") but this is not exactly what I am looking for because I dont want to specify the root doc. I want that it founds the tags part under each root. Commented Aug 28, 2013 at 15:13
  • And now I sorted out getNodeSet(doc, "//part") Commented Aug 28, 2013 at 15:27

1 Answer 1

1

I think the nodes returned by getNodeSet are pointers into the underlying XML object, so for instance

> special_nodes[[1]]
<name>ABC</name> 
> xpathSApply(special_nodes[[1]], "../cost")
[[1]]
<cost>3.54</cost> 
Sign up to request clarification or add additional context in comments.

3 Comments

I am not sure if I get you, but hasSentence = xpathApply(doc, "../part") gives me not a list of xml objects where each entry is a xml tree of the form <part> <name>ABC</name> <type>XYZ</type> <cost>3.54</cost> <status>available</status> </part>
But xpathSApply(special_nodes[[1]], "../../part") does? Actually I'm not following why 'doc' appears in relation to my answer, which operates on special_nodes[[1]]; maybe you found the answer to your original question in some other way.
know my second problem is still alive, it depends on the question how to transform the result of l<-getNodeSet(doc, "//part") for each entry in l to a list of nodes. Taht means I want all nodes for i>0 in l[i] in a list where each entry is a node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.