XML/XPath: Difference between revisions

Content added Content deleted
Line 3,114: Line 3,114:
=={{header|R}}==
=={{header|R}}==
{{libheader|XML (R)}}
{{libheader|XML (R)}}
<lang R>
<lang R>## Require the XML package you can download from http://www.omegahat.org/RSXML/
library("XML")
library("XML")
doc <- xmlInternalTreeParse("test3.xml")
doc <- xmlInternalTreeParse("test3.xml")

# 1st Task: Retrieve the first "item" element
# Retrieve the first "item" element
(firstItemElement <- getNodeSet(doc, "//item")[[1]])
getNodeSet(doc, "//item")[[1]]
# 2nd task: Perform an action on each "price" element (print it out)

prices <- sapply(getNodeSet(doc, "//price"), xmlValue)
# Perform an action on each "price" element
for(i in 1:length(prices)) print(prices[i])
sapply(getNodeSet(doc, "//price"), xmlValue)
# 3rd Task: Get an array of all the "name" elements

(namesArray <- sapply(getNodeSet(doc, "//name"), xmlValue))</lang>
# Get an array of all the "name" elements
sapply(getNodeSet(doc, "//name"), xmlValue)

</lang>


=={{header|Racket}}==
=={{header|Racket}}==