Skip to content
Prev 354111 / 398500 Next

Extracting XML value

Hi,

You are very close and your understanding is correct - you need to extract the root node from the XMLDocument returned from xmlTreeParse.

library(XML)

txt <-  "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<observations realtime_start=\"2015-09-03\" realtime_end=\"2015-09-03\" 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\">\n  <observation realtime_start=\"2015-09-03\" realtime_end=\"2015-09-03\" date=\"2015-09-01\" value=\"0.46\"/>\n</observations>\n\n\n\n"

# parse the text tree and extract the root node
obs <- xmlRoot(xmlTreeParse(txt, useInternalNodes = TRUE, asText = TRUE))

# get the first child node of 'observation' name.  Yes, there is just one.
obs1 <- obs['observation'][[1]]

# it has no value, just attributes of which 'value' is one
xmlAttrs(obs1)[['value']]


Cheers,
Ben
On Sep 3, 2015, at 11:23 AM, Glenn Schultz <glennmschultz at me.com> wrote:

            
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org