使用xml2在R中获取xll xpaths

时间:2017-05-26 11:59:58

标签: r xml2

在xml2中,可以使用以下方法获取给定节点的xpath:

xml_path

我想知道从给定文档中提取所有xpath的最快方法是什么。即我想找到最终节点,然后向上迭代。

本质上我试图实现这个目标:

library(xml2)

#Read
doc <- read_xml("http://www.w3schools.com/xml/plant_catalog.xml")

#Define a funciton to extract all xpaths:
extract_all_paths<-function(x){
  if (xml_length(x)==0){
    final_vector<-xml_path(x)
  } else{
    final_vector<-list("vector")
    i<-1
    while (length(x)!=0){
      x<-do.call(c,lapply(x,xml_children))
      x_length<-sapply(x,xml_length)
      final_vector[[i]]<-x[x_length==0]
      x<-x[x_length!=0]
      i<-i+1
    }
    final_vector<-do.call(c,final_vector)
    final_vector<-sapply(final_vector,xml_path)
    final_vector
  }
  final_vector
}

#Function to extract everything for a given xpath:
function_extract_values<-function(x,y){
  paste(xml_text(xml_find_all(y,x)),collapse="&&&&")
}

format_file<-function(x){
  x<-xml_ns_strip(x)
  data_path<-data.table(x=xml_children(x))
  data_xpath<-data_path[,extract_all_paths(x),by=1:nrow(data_path)]
  data_xpath[,V1:=gsub("\\[(.*?)\\]","",data_xpath$V1)]
  data_xpath<-data_xpath[!duplicated(V1)]
  data_xpath[,V2:=list(list(x)),by=1:nrow(data_xpath)]
  data_xpath[,value:=function_extract_values(V1,V2[[1]]),by=1:nrow(data_xpath)]
  data_xpath[,V1:=gsub("\\/","_",V1)]
  data_names<-data_xpath$V1
  data_xpath[,V1:=NULL]
  data_xpath[,nrow:=NULL]
  data_xpath[,V2:=NULL]
  data_xpath<-transpose(data_xpath)
  setnames(data_xpath,data_names)
  data_xpath
}
data<-format_file(doc)

本质上我想解析一个.xml文件,然后把它作为一行放到data.table中。我目前的解决方案非常慢,如果我有很多文件,也许有人可以建议更快的解决方案。

1 个答案:

答案 0 :(得分:2)

可能有更好的方法从文档中获取完整的xpath列表,但这是一个解决方案。 (也可能有更好的方法来迭代你的xml文档来获得你想要的东西,但是你要求提供所有xpath的列表):

library(XML) #This may work in xml2 but i usually stick with XML

#read document into R and select root
myXML <- xmlTreeParse("myXML.xml", useInternal = TRUE)
top <- xmlRoot(myXML)
#convert XML to list of lists
temp <- xmlToList(top)

#use names of recusive apply to get list of recusive steps through XML
temp <- unique(names(rapply(test, summary, how="unlist")))
#remove the last item created by summary function
temp <- unique(sub("\\.[^.]*$", "", temp))
#remove attributes
temp <- unique(sub("..attrs", "", temp))
#sub . for / to create xpath
temp <- sub("\\.","/", temp)
#add / to start the xpath at the docuemnt root
XPaths <- paste0("/", temp)