生成函数调用以使用R修改xml

时间:2018-09-07 11:47:05

标签: r xml2

我想使用R和xml2动态修改XML文档:我的方法是将参数准备好放入数据框中,然后将这些参数传递给可以修改xml文档的函数。

library(xml2)

doc <- xml2::read_xml('<CATALOG>
<PLANT myid="1">
  <COMMON>Bloodroot</COMMON>
  <BOTANICAL>Sanguinaria canadensis</BOTANICAL>
  <ZONE>4</ZONE>
  <LIGHT>Mostly Shady</LIGHT>
  <PRICE>$2.44</PRICE>
  <AVAILABILITY>031599</AVAILABILITY>
  </PLANT>
  <PLANT myid="2">
  <COMMON>Columbine</COMMON>
  <BOTANICAL>Aquilegia canadensis</BOTANICAL>
  <ZONE>3</ZONE>
  <LIGHT>Mostly Shady</LIGHT>
  <PRICE>$9.37</PRICE>
  <AVAILABILITY>030699</AVAILABILITY>
  </PLANT>
  <PLANT myid="3">
  <COMMON>Marsh Marigold</COMMON>
  <BOTANICAL>Caltha palustris</BOTANICAL>
  <ZONE>4</ZONE>
  <LIGHT>Mostly Sunny</LIGHT>
  <PRICE>$6.81</PRICE>
  <AVAILABILITY>051799</AVAILABILITY>
  </PLANT>
  <PLANT myid="4">
  <COMMON>Cowslip</COMMON>
  <BOTANICAL>Caltha palustris</BOTANICAL>
  <ZONE>4</ZONE>
  <LIGHT>Mostly Shady</LIGHT>
  <PRICE>$9.90</PRICE>
  <AVAILABILITY>030699</AVAILABILITY>
  </PLANT>
  <PLANT myid="5">
  <COMMON>Dutchman\'s-Breeches</COMMON>
  <BOTANICAL>Dicentra cucullaria</BOTANICAL>
  <ZONE>3</ZONE>
  <LIGHT>Mostly Shady</LIGHT>
  <PRICE>$6.44</PRICE>
  <AVAILABILITY>012099</AVAILABILITY>
  </PLANT>
  <PLANT myid="6">
  <COMMON>Ginger, Wild</COMMON>
  <BOTANICAL>Asarum canadense</BOTANICAL>
  <ZONE>3</ZONE>
  <LIGHT>Mostly Shady</LIGHT>
  <PRICE>$9.03</PRICE>
  <AVAILABILITY>041899</AVAILABILITY>
  </PLANT>
  <PLANT myid="7">
  <COMMON>Hepatica</COMMON>
  <BOTANICAL>Hepatica americana</BOTANICAL>
  <ZONE>4</ZONE>
  <LIGHT>Mostly Shady</LIGHT>
  <PRICE>$4.45</PRICE>
  <AVAILABILITY>012699</AVAILABILITY>
  </PLANT>
  </CATALOG>'

)


xpath <-  "//PLANT[@myid='2']"
xml_find_all(doc,xpath)

newxml <- function(xmldoc, xpath, newattr) {

  #
  VarName <- xml2::xml_find_all(xmldoc, xpath)
  xml_attr(VarName, "Name") <- newattr

}

现在,如果要基于id修改由数千种植物组成的XML文档(我可以将xpath和新值存储在数据帧或向量中),应该如何进行?

我尝试了以下操作:

do.call(newxml,list(xmldoc = doc, xpath= dftest$xpath, 
                       newattr = dftest$newname))

但是它不起作用,它告诉我:

 Error in xpath_search(x$node, x$doc, xpath = xpath, nsMap = ns, num_results = Inf) : 
  Expecting a single string value: [type=character; extent=5]. 

关于R,我想必有些大事,但我不知道是什么,请随时提出一个更好的方法来解决这个问题。

1 个答案:

答案 0 :(得分:1)

问题似乎是将您的dataframe列作为函数参数传入。

如果我传入单个参数,该函数似乎会提供结果:

xpath <-  "//PLANT[@myid='2']"
xml_find_all(doc,xpath)
newxml <- function(xmldoc, xpath, newattr) {

  #
  VarName <- xml2::xml_find_all(xmldoc, xpath)
  xml_attr(VarName, "Name") <- newattr

}
do.call(newxml,list(xmldoc = doc, xpath= xpath, 
                    newattr = "TESTING"))

以下产生:

 xml_find_all(doc,xpath)
{xml_nodeset (1)}
[1] <PLANT myid="2" Name="TESTING">\n  <COMMON>Columbine</COMMON>\n

我认为您数据框中的dtype可能不是字符串因素

我使用了以下内容:

newxml <- function(xpath, newattr,xmldoc=doc) {
  VarName <- xml2::xml_find_all(xmldoc, as.character(xpath))
  xml_attr(VarName, "Name") <- as.character(newattr)
}

df <- data.frame(a= c(xpath,xpath),b = c("TEST1","TEST2"))
mapply(newxml,df$a, df$b)

use multiple columns as variables with sapply