R Cran – Nested irregular xml with xml’s attributes as dataframe’s col names

I have a big xml file produced by windows log and i need to import it into R to analyse some correlation beetween the events logged.
The problem i encounter is that the events’details (EventData) are kept as xml’s attributes and the number of attributes changes by the kind of the events.

The simplified xml to import is:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Events>
    <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
        <System>
            <EventID>259</EventID>
            <Version>0</Version>
            <Level>2</Level>
            <TimeCreated SystemTime="2024-07-10T15:02:35.203376200Z"/>
        </System>
        <EventData>
            <Data Name="TCP">0</Data>
            <Data Name="InterfaceIP">0.0.0.0</Data>
            <Data Name="Source">192.168.109.4</Data>
            <Data Name="QNAME"/>
        </EventData>
    </Event>
    <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
        <System>
            <EventID>256</EventID>
            <Version>0</Version>
            <Level>4</Level>
            <TimeCreated SystemTime="2024-07-10T15:02:35.203362300Z"/>
        </System>
        <EventData>
            <Data Name="TCP">0</Data>
            <Data Name="InterfaceIP">0.0.0.0</Data>
            <Data Name="Source">192.168.109.4</Data>
            <Data Name="QNAME">microsoft.com.</Data>
            <Data Name="QTYPE">0</Data>
            <Data Name="XID">22342</Data>
            <Data Name="Port">65220</Data>
            <Data Name="Flags">16979</Data>
            <Data Name="BufferSize">108</Data>
         </EventData>
    </Event>
</Events>

This is what I expect.

The code I wrote is:

library(xml2)
library(tibble)
library(dplyr)


file_dns <- c("./test_ridotto_2.xml")
read_xml(file_dns) -> dns


dns_items <- tibble(
   event_id =  xml_find_all(x = dns, xpath = "/Events/Event/System/EventID") |> xml_text(),
   time_created = xml_find_all(x = dns, xpath = "/Events/Event/System/TimeCreated") |> xml_attr(attr = "SystemTime"),
   info4 = xml_find_all(x = dns, xpath = "/Events/Event/EventData") |> as_list()
)

The problem I have, is that I don’t understand how to continue to split columns and give them the name of the attribute, as the number of attributes and so the number of columns change row by row and depend from the type of the event logged.
I tried the unnest_wider command but it lost attributes and seems slow on my simplified test file.
Production file is 1Gb.

How to obtain the expected result?

2

This script extracts out the eventID and createdtime from each event and then loops through the events and extracts out the data elements and node ID to create a dataframe for your final answer.

See the comments below for a step-by-step.

library(xml2)
library(dplyr)

#read page and strip namespace
page <- read_xml(text)
xml_ns(page)

#find the events nodes
events <- xml_find_all(page, ".//Event")

#get the eventID and Created time
eventID <- xml_find_first(events, ".//EventID") %>% xml_text()
CreatedTime <-xml_find_first(events, ".//TimeCreated") %>% xml_attr("SystemTime")

#loop through the events and extract the data
#make a data frame and create the proper column headings
dfs <- lapply(events, function(node){
   data <- xml_find_all(node, ".//Data") %>% xml_text()
   tag <- xml_find_all(node, ".//Data") %>% xml_attrs() %>% unlist()
   names(tag)<-NULL
   df<-as.data.frame(t(data))
   names(df) <- tag
   df
})

#make a data frame for the answer
answer <- bind_rows(dfs)

#add the event ID and timecreated
answer <-bind_cols(EventID=eventID, CreatedTime=CreatedTime, answer)

  EventID                    CreatedTime TCP InterfaceIP        Source          QNAME QTYPE   XID  Port Flags BufferSize
1     259 2024-07-10T15:02:35.203376200Z   0     0.0.0.0 192.168.109.4                 <NA>  <NA>  <NA>  <NA>       <NA>
2     256 2024-07-10T15:02:35.203362300Z   0     0.0.0.0 192.168.109.4 microsoft.com.     0 22342 65220 16979        108

An approach using xml2::as_list. The attributes of the elements stay intact even when they are put in a dataframe/tibble with as_tibble.

The performance will be limited by purrr since it has to process columns element-wise.

library(xml2)
library(dplyr)
library(tidyr)

as_tibble(as_list(read_xml("event.xml"))) %>% 
  unnest_wider(Events) %>% 
  mutate(EventID = purrr::map_vec(System, ~ unlist(.x$EventID)), 
         TimeCreated = purrr::map_vec(System, ~ attr(.x$TimeCreated, "SystemTime"))) %>%
  select(-System) %>% 
  unnest(EventData) %>% 
  mutate(name = purrr::map_vec(EventData, ~ unlist(attributes(.x))), 
         value = purrr::map_vec(EventData, ~ toString(.x)), EventData = NULL) %>% 
  pivot_wider()

output

# A tibble: 2 × 11
  EventID TimeCreated     TCP   InterfaceIP Source QNAME QTYPE XID   Port  Flags
  <chr>   <chr>           <chr> <chr>       <chr>  <chr> <chr> <chr> <chr> <chr>
1 259     2024-07-10T15:… 0     0.0.0.0     192.1… ""    NA    NA    NA    NA
2 256     2024-07-10T15:… 0     0.0.0.0     192.1… "mic… 0     22342 65220 16979
# ℹ 1 more variable: BufferSize <chr>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật