Spencer Trinh
Spencer Trinh

Reputation: 783

How to extract specific elements from XML node

I have something like this:

<ValuesPeaks>
  <Peak Start="244" Stop="248" Max="245" XValue="149" YValue="100.0000"/>
  <Peak Start="361" Stop="368" Max="366" XValue="173.2" YValue="96.2713"/>
<ValuesPeaks>

Except they are a lot longer and I have about 300 sets of <ValuesPeaks>. How can I extract only the XValue and YValue elements of everything? I thought I can do xpathSApply('//ValuesPeaks[XValue]',xmlValue), but its not working. I then thought I can do toString.XMLNode() then use regexpr() and substr() to obtain what I want but that seems inefficient. I think I'm missing something. Please share your expertise. Thanks.

p<-list.files()[[1]]
library(XML)
x<-xmlParse(p)
getNodeSet(x,'//Data/RESULT/*/*/*/ValuesPeaks/Peak')
f<-xpathSApply(x,'//Data/RESULT/*/*/*/ValuesPeaks/Peak')
t<-toString.XMLNode(f)

Upvotes: 1

Views: 1865

Answers (2)

Rich Scriven
Rich Scriven

Reputation: 99331

There are a few ways to extract those attributes. It all depends on what you want the result to look like. Here are a couple of examples.

The first uses xmlAttrs() and subsets the results.

xpathApply(doc, "//ValuesPeaks//*", function(x) xmlAttrs(x)[c("XValue", "YValue")])
# [[1]]
#     XValue     YValue 
#      "149" "100.0000" 
#
# [[2]]
#    XValue    YValue 
#   "173.2" "96.2713" 

The second is likely more efficient. It uses an XPath statement to get the two relevant attributes.

xpathSApply(doc, "//ValuesPeaks//@*[name()='XValue' or name()='YValue']")
#    XValue     YValue     XValue     YValue 
#     "149" "100.0000"    "173.2"  "96.2713" 

You could even do

sapply(unname(xmlToList(doc)), "[", c("XValue", "YValue"))
#        [,1]       [,2]     
# XValue "149"      "173.2"  
# YValue "100.0000" "96.2713"

Data:

txt <- '<ValuesPeaks>
  <Peak Start="244" Stop="248" Max="245" XValue="149" YValue="100.0000"/>
  <Peak Start="361" Stop="368" Max="366" XValue="173.2" YValue="96.2713"/>
</ValuesPeaks>'
library(XML)
doc <- xmlParse(txt)

Upvotes: 2

alistaire
alistaire

Reputation: 43334

Your XML is malformed (the second ValuePeaks tag needs a / to make it a closing tag), which causes xml2::read_xml to complain. read_html actually automatically fixes it though, so you can do

library(xml2)
library(tidyverse)

x <- '<ValuesPeaks>
  <Peak Start="244" Stop="248" Max="245" XValue="149" YValue="100.0000"/>
  <Peak Start="361" Stop="368" Max="366" XValue="173.2" YValue="96.2713"/>
<ValuesPeaks>' 

df <- x %>% 
    read_html() %>% 
    xml_find_all('//peak') %>% {
        data_frame(xvalue = xml_attr(., 'xvalue'), 
                   yvalue = xml_attr(., 'yvalue'))
    } %>% 
    type_convert()

df
#> # A tibble: 2 x 2
#>   xvalue   yvalue
#>    <dbl>    <dbl>
#> 1  149.0 100.0000
#> 2  173.2  96.2713

Upvotes: 2

Related Questions