weddegege
weddegege

Reputation: 35

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

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.

Upvotes: 1

Views: 267

Answers (1)

akrun
akrun

Reputation: 887098

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

Upvotes: 1

Related Questions