XML/XPath: Difference between revisions

Content added Content deleted
No edit summary
Line 264: Line 264:
doc <- xmlInternalTreeParse("test3.xml")
doc <- xmlInternalTreeParse("test3.xml")
# 1st Task: Retrieve the first "item" element
# 1st Task: Retrieve the first "item" element
(firstItemElement <- getNodeSet(doc,"//item")[[1]])
(firstItemElement <- getNodeSet(doc, "//item")[[1]])
# 2nd task: Perform an action on each "price" element (print it out)
# 2nd task: Perform an action on each "price" element (print it out)
prices <- sapply(getNodeSet(doc,"//price"),xmlValue)
prices <- sapply(getNodeSet(doc, "//price"), xmlValue)
for(i in 1:length(prices)) print(prices[i])
for(i in 1:length(prices)) print(prices[i])
# 3rd Task: Get an array of all the "name" elements
# 3rd Task: Get an array of all the "name" elements
(namesArray <- sapply(getNodeSet(doc,"//name"),xmlValue))
(namesArray <- sapply(getNodeSet(doc, "//name"), xmlValue))


</lang>
</lang>