R 并非所有父节点都包含子节点的XML数据提取

R 并非所有父节点都包含子节点的XML数据提取,r,xml,R,Xml,我有一个xml数据文件,用户在其中开立了一个帐户,在某些情况下,该帐户已被终止。帐户未终止时,数据未列出值,这使得提取信息非常困难 以下是可复制的示例(其中只有用户1和3的帐户被终止): 关于如何解决这个问题有什么建议吗?我设法从 accounts我更喜欢使用xml2包而不是XML包,我发现语法更容易使用。这是一个直截了当的问题。找到所有用户节点,然后解析出id和终止节点。对于xml2,如果没有找到节点,xml\u find\u first函数将返回NA library(xml2) my_xml

我有一个xml数据文件,用户在其中开立了一个帐户,在某些情况下,该帐户已被终止。帐户未终止时,数据未列出值,这使得提取信息非常困难

以下是可复制的示例(其中只有用户1和3的帐户被终止):


关于如何解决这个问题有什么建议吗?

我设法从


accounts我更喜欢使用xml2包而不是XML包,我发现语法更容易使用。这是一个直截了当的问题。找到所有用户节点,然后解析出id和终止节点。对于xml2,如果没有找到节点,
xml\u find\u first
函数将返回NA

library(xml2)
my_xml <- read_xml('<accounts>
                   <user>
                   <id>1</id>
                   <start>2015-01-01</start>
                   <termination>2015-01-21</termination>
                   </user>
                   <user>
                   <id>2</id>
                   <start>2015-01-01</start>
                   </user>
                   <user>
                   <id>3</id>
                   <start>2015-02-01</start>
                   <termination>2015-04-21</termination>
                   </user>
                   <user>
                   <id>4</id>
                   <start>2015-03-01</start>
                   </user>
                   <user>
                   <id>5</id>
                   <start>2015-04-01</start>
                   </user>
                   </accounts>')

usernodes<-xml_find_all(my_xml, ".//user")
ids<-sapply(usernodes, function(n){xml_text(xml_find_first(n, ".//id"))})
terms<-sapply(usernodes, function(n){xml_text(xml_find_first(n, ".//termination"))})

answer<-data.frame(ids, terms)
库(xml2)
我的xml
accounts <- data.frame(id=sapply(my_xml["//user//id"], xmlValue),
                       start=sapply(my_xml["//user//start"], xmlValue),
                       termination=sapply(my_xml["//user//termination"], xmlValue)
                       )
accounts <- data.frame(id=sapply(my_xml["//user//id"], xmlValue),
                       start=sapply(my_xml["//user//start"], xmlValue),
                       termination=sapply(xpathApply(my_xml, "//user",
                                                     function(x){
                                                     if("termination" %in% names(x))
                                                     xmlValue(x[["termination"]])
                                                     else NA}), function(x) x))
library(xml2)
my_xml <- read_xml('<accounts>
                   <user>
                   <id>1</id>
                   <start>2015-01-01</start>
                   <termination>2015-01-21</termination>
                   </user>
                   <user>
                   <id>2</id>
                   <start>2015-01-01</start>
                   </user>
                   <user>
                   <id>3</id>
                   <start>2015-02-01</start>
                   <termination>2015-04-21</termination>
                   </user>
                   <user>
                   <id>4</id>
                   <start>2015-03-01</start>
                   </user>
                   <user>
                   <id>5</id>
                   <start>2015-04-01</start>
                   </user>
                   </accounts>')

usernodes<-xml_find_all(my_xml, ".//user")
ids<-sapply(usernodes, function(n){xml_text(xml_find_first(n, ".//id"))})
terms<-sapply(usernodes, function(n){xml_text(xml_find_first(n, ".//termination"))})

answer<-data.frame(ids, terms)