在R中如何从公共父节点配对XML节点值?

时间:2016-09-11 07:19:54

标签: r xml xml-parsing

我有以下示例XML:

<body>
  <div class="row">
    <div class="column">
      <span class="title">Color</span>
    </div>
    <div class="column property">Blue</div>
  </div> 
  <div class="row">
    <div class="column">
      <span class="title">Shape</span>
    </div>
    <div class="column property">Square</div>
  </div> 
</body>

我如何使用R将每个标题与其属性和输出配对:

Color = Blue
Shape = Square

我尝试了下面的脚本,但标题周围有XML标签,缺少属性:

library(XML)

getDetails <- function(id) {
  html <- htmlTreeParse( "exampleXML.html" ,useInternal = TRUE)
  xpathSApply( html , "//div[@class='row']" , function(row) { 
    print( xmlElementsByTagName(row, "span", recursive = TRUE) )
  })
}

getDetails()

也没有运气:

library(XML)      #to install use: install.packages("XML")
library(xml2)     #to install use: install.packages("xml2")
library(magrittr) #to install use: install.packages("magrittr")

extract_info <- function(x){
   title <- x %>% xml_find_first(".//span[@class='title']") %>% xml_text
   property <- x %>% xml_find_first(".//div[@class='column property']") %>% xml_text
   setNames(property, title)
 }

html <- htmlTreeParse( "exampleXML.html" ,useInternal = TRUE)
html %>% xml_find_all("//div[@class='row']") %>% extract_info
  

UseMethod出错(&#34; xml_find_all&#34;):         没有适用于&#39; xml_find_all&#39;的方法应用于类&#34; c的对象(&#39; HTMLInternalDocument&#39;,&#39; HTMLInternalDocument&#39;,&#39; XMLInternalDocument&#39;,&#39; XMLAbstractDocument&#39;)&# 34;

3 个答案:

答案 0 :(得分:1)

使用xml2,您可以执行以下操作:

library(xml2)     #to install use: install.packages("xml2")
library(magrittr) #to install use: install.packages("magrittr")

extract_info <- function(x){
  title <- x %>% xml_find_first(".//span[@class='title']") %>% xml_text
  property <- x %>% xml_find_first(".//div[@class='column property']") %>% xml_text
  setNames(property, title)
}

html <- read_xml( "exampleXML.html" )
html %>% xml_find_all("//div[@class='row']") %>% extract_info

它为您提供以下命名向量:

   Color    Shape 
  "Blue" "Square"

答案 1 :(得分:1)

如果您的XML格式正确(即元素的顺序没有改变),那么您可以这样做:

library(xml2)
library(purrr)

doc <- read_xml(txt)

vals <- xml_text(xml_find_all(doc, ".//*[@class='title' or @class='column property']"))
map_chr(seq(1, length(vals), by=2), ~sprintf("%s = %s", vals[.], vals[.+1])) %>% 
  cat(sep="\n")

答案 2 :(得分:1)

考虑使用嵌套的xpathSApply(),其中外部循环遍历,以解析每行 title property 的相应值:

library(XML)

example_html <- paste0('<body>',
                   '  <div class="row">',
                   '    <div class="column">',
                   '       <span class="title">Color</span>',
                   '    </div>',
                   '    <div class="column property">Blue</div>',
                   '  </div>',
                   '  <div class="row">',
                   '    <div class="column">',
                   '       <span class="title">Shape</span>',
                   '    </div>',
                   '    <div class="column property">Square</div>',
                   '  </div>', 
                   '</body>')

doc <- htmlTreeParse(example_html, useInternal = TRUE)

columns <- xpathSApply(doc, "//div[@class='row']", function(row){
   title <- xpathSApply(row, "div[@class='column']/span", xmlValue)
   property <- xpathSApply(row, "div[@class='column property']", xmlValue)
   setNames(gsub(" ", "", property), gsub(" ", "", title))    # GSUB TO STRIP WHITESPACE
})

columns <- setNames(property, title)
columns
#  Color    Shape 
#  "Blue" "Square" 

或者,假设中的严格一致性而不丢失子元素或 title property 值的多个相同命名元素,请考虑几个xpathSApply()来电:

title <- xpathSApply(doc, "//div[@class='column']/span", xmlValue)
property <- xpathSApply(doc, "//div[@class='column property']", xmlValue)

columns <- setNames(property, title)
columns
#   Color    Shape 
#  "Blue" "Square"