简体   繁体   English

在 R 中将 XML 转换为数据框

[英]Convert XML to Data Frame in R

Hi I am trying to convert the following XML code to a data frame in R. However I can't because there are values missing per record.您好我正在尝试将以下 XML 代码转换为 R 中的数据框。但是我不能,因为每条记录都缺少值。

RecordID 23063 has the following data with it ActivityCreatedDate, ExpectedInstallDate, InvoiceTxnDate. RecordID 23063 包含以下数据:ActivityCreatedDate、ExpectedInstallDate、InvoiceTxnDate。 However some the following nodes do not have all of these elements to them.但是,以下一些节点并不具备所有这些元素。 RecordID 23321 is missing InvoiceTxnDate, etc. RecordID 23321 缺少 InvoiceTxnDate 等。

<?xml version="1.0" encoding="windows-1252" ?>
  <Record>
    <RecordID>23063</RecordID>
    <ActivityCreatedDate>2018-12-11T19:00:00</ActivityCreatedDate>
    <ExpectedInstallDate>2018-12-19T19:00:00</ExpectedInstallDate>
    <InvoiceTxnDate>2018-12-13T19:00:00</InvoiceTxnDate>
  </Record>
  <Record>
    <RecordID>23321</RecordID>
    <ActivityCreatedDate>2018-10-15T18:00:00</ActivityCreatedDate>
    <ExpectedInstallDate>2018-11-14T19:00:00</ExpectedInstallDate>
  </Record>
  <Record>
    <RecordID>23566</RecordID>
    <ActivityCreatedDate>2019-01-23T19:00:00</ActivityCreatedDate>
  </Record>
  <Record>
    <RecordID>23217</RecordID>
    <ActivityCreatedDate>2018-12-20T19:00:00</ActivityCreatedDate>
    <ExpectedInstallDate>2019-01-23T19:00:00</ExpectedInstallDate>
    <InvoiceTxnDate>2019-01-18T19:00:00</InvoiceTxnDate>
  </Record>
  <Record>
    <RecordID>23325</RecordID>
    <ActivityCreatedDate>2018-05-25T18:00:00</ActivityCreatedDate>
    <ExpectedInstallDate>2019-01-23T19:00:00</ExpectedInstallDate>
  </Record>
</end of file>

currently I am using xml2.目前我正在使用 xml2。 I am using read_xml to read it to a variable, and then xml_find_all and trimws to store the column to a list.我正在使用 read_xml 将其读取到变量中,然后使用 xml_find_all 和 trimws 将列存储到列表中。 I then attempt to turn my list into a data frame, but it fails because the dimensions are off.然后我尝试将我的列表转换为数据框,但由于维度关闭而失败。

I want to know how I can turn the above XML into a data frame that looks like this:我想知道如何将上述 XML 转换为如下所示的数据框:

RecordID    ActivityCreatedDate ExpectedInstallDate InvoiceTxnDate
1   23063   2018-12-11T19:00:00 2018-12-19T19:00:00 2018-12-13T19:00:00
2   23321   2018-10-15T18:00:00 2018-11-14T19:00:00 NA
3   23566   2019-01-23T19:00:00 NA                  NA
4   23217   2018-12-20T19:00:00 2019-01-23T19:00:00 2019-01-18T19:00:00
5   23325   2018-05-25T18:00:00 2019-01-23T19:00:00 NA

Is there a way to loop through each RecordID in this case and add a在这种情况下,有没有办法遍历每个 RecordID 并添加一个

<InvoiceTxnDate>NA</InvoiceTxnDate> or a <ExpectedInstallDate>NA</ExpectedInstallDate>

to the node if its missing?到节点,如果它丢失? I'd be more then happy to share the R code I have for data that's all uniform.我会更乐意分享我拥有的所有统一数据的 R 代码。 Also if this question does not make sense please let me know and I will explain myself more.另外,如果这个问题没有意义,请告诉我,我会更多地解释自己。

Have you tried using the XML package?您是否尝试过使用XML包?

XML::xmlToDataFrame('path to xml file')


> XML::xmlToDataFrame('~/R/test.xml')
  RecordID ActivityCreatedDate ExpectedInstallDate      InvoiceTxnDate
1    23063 2018-12-11T19:00:00 2018-12-19T19:00:00 2018-12-13T19:00:00
2    23321 2018-10-15T18:00:00 2018-11-14T19:00:00                <NA>
3    23566 2019-01-23T19:00:00                <NA>                <NA>
4    23217 2018-12-20T19:00:00 2019-01-23T19:00:00 2019-01-18T19:00:00
5    23325 2018-05-25T18:00:00 2019-01-23T19:00:00                <NA>

In the case that the XML is exactly as shown above, with no root node.在 XML 完全如上所示的情况下,没有根节点。 You can do the following:您可以执行以下操作:

library(xml2)
library(rvest)
library(tidyverse)

## METHOD 1
## add missing root node
read_html('~/R/test.xml') %>% html_children() %>% 
  as_xml_document(root = 'doc') %>% xml_contents() %>% xml_contents() %>% 
  map_df(., function(x) {
    kids <- xml_children(x)
    setNames(as.list(type.convert(xml_text(kids))), xml_name(kids))
  })

## METHOD 2
## treating the xml as a list
read_html('~/R/test.xml') %>% 
  html_nodes('record') %>% 
  as_list() %>% 
  lapply(., function(x) unlist(x, recursive = F) %>% bind_cols()) %>% 
  bind_rows()


## both of the above methods will return the following tibble
# A tibble: 5 x 4
  recordid activitycreateddate expectedinstalldate invoicetxndate     
  <chr>    <chr>               <chr>               <chr>              
1 23063    2018-12-11T19:00:00 2018-12-19T19:00:00 2018-12-13T19:00:00
2 23321    2018-10-15T18:00:00 2018-11-14T19:00:00 NA                 
3 23566    2019-01-23T19:00:00 NA                  NA                 
4 23217    2018-12-20T19:00:00 2019-01-23T19:00:00 2019-01-18T19:00:00
5 23325    2018-05-25T18:00:00 2019-01-23T19:00:00 NA  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM