如何用 R 解析 USER_DEFINED XML 数据

How to parse USER_DEFINED XML data with R

我有一个 XML 文件,其中包含我正在尝试解析的 USER_DEFINED 参数。这是 XML 文档的示例。

         <userDefinedParameters>
           <USER_DEFINED parameter="P1">LEFT</USER_DEFINED>
           <USER_DEFINED parameter="P2">RIGHT</USER_DEFINED>
           <USER_DEFINED parameter="P3">1234</USER_DEFINED>
           <USER_DEFINED parameter="P4">5678</USER_DEFINED>
         </userDefinedParameters>
       </data>
     </segment>
   </body>
</head>

我能够使用 XML 包和 xpathApply 从该文件中解析出所有数据。但是,我无法通过这种方式提取 USER_DEFINED 参数值。

由于 XML 中有几条记录,我想获取所有 P1、P2 等,就像我使用 xpathApply 获取其他字段一样。 document 声明所有 USER_DEFINED 参数为 'parameter' 和 'value' 所以我想我需要拉为 c('parameter', 'value') 但我不知道该怎么做使用 XML.

我看过this SO page,很有帮助,但没有回答这个问题。

感谢 any/all 帮助。

更新了所需的输出以及我如何尝试获取数据。请注意,以下代码无法正常工作。

当前 xpathApply 用法获取 userDefinedParameters 部分中的所有 USER_DEFINED 行。如果我更改为 xpathApply(data, "//USER_DEFINED"), xmlValue),那么我将获得所有值,但与参数名称无关。我需要类似 xpathApply(data, "//USER_DEFINED/P1"), xmlValue) 的东西,但显然,这不起作用。

Library(XML)
fileName <- "./file.xml"
data     <- xmlParse(fileName)
xml_data <- xmlToList(data)
p1 <- xpathApply(data, "//USER_DEFINED")
p2 <- xpathApply(data, "//USER_DEFINED")

# View(p1)
#     "P1"
#     LEFT
#     LEFT
#    RIGHT

# View(p2)
#     "P2"
#    RIGHT
#    RIGHT
#     LEFT
# ...

使用 xml2 库,您可以使用 xml_attr().

从节点获取 parameter 的值

像这样:

library(xml2)

x <- read_xml('<userDefinedParameters>
       <USER_DEFINED parameter="P1">LEFT</USER_DEFINED>
       <USER_DEFINED parameter="P2">right</USER_DEFINED>
       <USER_DEFINED parameter="P3">1234</USER_DEFINED>
       <USER_DEFINED parameter="P4">5678</USER_DEFINED>
     </userDefinedParameters>')

dataset <- data.frame(user_defined = x %>% 
                                       xml_find_all("//USER_DEFINED") %>%
                                       xml_text(),
                      parameter = x %>% 
                                    xml_find_all("//USER_DEFINED") %>%
                                    xml_attr("parameter"))

结果dataset

  user_defined parameter
1         LEFT        P1
2        right        P2
3         1234        P3
4         5678        P4

如果您喜欢使用 XML 包,可以使用 sapply

中的 xmlAttrs 函数
text <-' <head> <body> <segment>
 <data>
 <userDefinedParameters>
           <USER_DEFINED parameter="P1">LEFT</USER_DEFINED>
           <USER_DEFINED parameter="P2">right</USER_DEFINED>
           <USER_DEFINED parameter="P3">1234</USER_DEFINED>
           <USER_DEFINED parameter="P4">5678</USER_DEFINED>
         </userDefinedParameters>
       </data>
     </segment>
   </body>
</head>'

library(XML)
#read the document
doc <- xmlRoot(xmlParse(text))

#parse out the USER Defined nodes
# in this example there are 4 nodes
nodes<-xpathApply(doc, ".//userDefinedParameters/USER_DEFINED")

#step through each of the found nodes
# xmlAttrs is not a vectorized function thus requiring a loop
attributes <- sapply(nodes, function(n) {
   #extract the attribute from each node
   # if there was more than 1 attribute this will need updating
   xmlAttrs(unlist(n)) })

#get values from each node
values<-xmlValue(nodes)

data.frame(attributes, values)
#   attributes values
# 1         P1   LEFT
# 2         P2  right
# 3         P3   1234
# 4         P4   5678