[R] extracting a value from XML

boB Rudis bob at rudis.net
Mon Sep 21 21:46:38 CEST 2015


The "<observation ..." format of the tag tells us it's a [child] node
in the document and the "//observation" XPath expression targets _all_
"observation" nodes (sometimes that's acceptable, sometimes you need
to be more specific in the XPath expression you use). The 'value="...'
part of the tag is an attribute of the tag, hence using `xml_attr()`
or `xmlGetAttr` to retrieve it from the extracted node(s)

On Mon, Sep 21, 2015 at 3:41 PM, Glenn Schultz <glennmschultz at me.com> wrote:
> 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