Home > Enterprise >  Using XPath to get element value with XML package (R)
Using XPath to get element value with XML package (R)

Time:10-17

Say I have the following XML snippet:

<book>
  <title lang="en">Harry Potter</title>
  <price>29.99</price>
</book>

How can I get the price value? I've tried variations of this snippet using XPath:

library("XML")
price = xpathSApply(doc, '//book/price')

but it's not returning 29.99 as I expected.

CodePudding user response:

We can read with read_xml

library(xml2)
dat <- read_xml("doc1.xml")
as.numeric(xml_text(xml_find_all(dat, "price")))
[1] 29.99
  • Related