[R] extracting a value from XML

Glenn Schultz glennmschultz at me.com
Mon Sep 21 21:41:47 CEST 2015


Hi Bob,

Thanks, can you help me undestand why it starts with //observation and how you know that it is a node?

Glenn

On Sep 21, 2015, at 01:56 PM, boB Rudis <bob at rudis.net> wrote:

This is how (one way) in both the xml2 package and XML package:

library(xml2)
library(XML)

txt <- '<?xml version="1.0" encoding="utf-8"?>
<observations realtime_start="2015-09-21" realtime_end="2015-09-21"
observation_start="2015-09-01" observation_end="2015-09-01"
units="lin" output_type="1" file_type="xml"
order_by="observation_date" sort_order="asc" count="1" offset="0"
limit="100000">
<observation realtime_start="2015-09-21" realtime_end="2015-09-21"
date="2015-09-01" value="0.46"/>
</observations>'

doc <- read_xml(txt)
xml_attr(xml_find_all(doc, "//observation"), "value")

doc1 <- xmlParse(txt)
xpathSApply(doc1, "//observation", xmlGetAttr, "value")



On Mon, Sep 21, 2015 at 2:01 PM, Glenn Schultz <glennmschultz at me.com> wrote:
<?xml version="1.0" encoding="utf-8"?>
<observations realtime_start="2015-09-21" realtime_end="2015-09-21"
observation_start="2015-09-01" observation_end="2015-09-01" units="lin"
output_type="1" file_type="xml" order_by="observation_date" sort_order="asc"
count="1" offset="0" limit="100000">
<observation realtime_start="2015-09-21" realtime_end="2015-09-21"
date="2015-09-01" value="0.46"/>
</observations>


More information about the R-help mailing list