Q:

Import and transform multiple xml files into DF

I am trying to create a routine for importing numerous xml files from a given directory. Possibly I will have to import over a thousand xml files at one time and turn them into dataframe. I have already created the import routine from a single file:
require(tidyverse)
require(xml2)
setwd("D:/")
page<- read_xml("base.xml")
ns<- page %>% xml_find_all(".//test:billing")
billing<-xml2::as_list(ns) %>% jsonlite::toJSON() %>% jsonlite::fromJSON()

My exemple xml (base.xml):
<?xml version="1.0" encoding="ISO-8859-1" ?>

<test:TASS xmlns="http://www.vvv.com/schemas"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.vvv.com/schemas http://www.vvv.com/schemas/testV2_02_03.xsd"  xmlns:test="http://www.vvv.com/schemas" >
    <test:house>
                <test:billing>
                    <test:proceduresummary>
                        <test:guidenumber>X2030</test:guidenumber>
                            <test:diagnosis>
                                <test:table>ICD-10</test:table>
                                <test:diagnosiscod>J441</test:diagnosiscod>
                                <test:description>CHRONIC OBSTRUCTIVE PULMONARY DISEASE WITH (ACUTE) EXACERBATION</test:description>
                            </test:diagnosis>
                            <test:procedure>
                                <test:procedure>
                                    <test:description>HOSPITAL</test:description>
                                </test:procedure>
                                <test:amount>12</test:amount>
                            </test:procedure>
                    </test:proceduresummary>
                </test:billing>
                    <test:billing>
                    <test:proceduresummary>
                        <test:guidenumber>Y6055</test:guidenumber>
                            <test:diagnosis>
                                <test:table>ICD-10</test:table>
                                <test:diagnosiscod>I21</test:diagnosiscod>
                                <test:description>ACUTE MYOCARDIAL INFARCTION</test:description>
                            </test:diagnosis>
                            <test:procedure>
                                <test:procedure>
                                    <test:description>HOSPITAL</test:description>
                                </test:procedure>
                                <test:amount>8</test:amount>
                            </test:procedure>
                    </test:proceduresummary>
                </test:billing>
                    <test:billing>
                    <test:proceduresummary>
                        <test:guidenumber>Z9088</test:guidenumber>
                            <test:diagnosis>
                                <test:table>ICD-10</test:table>
                                <test:diagnosiscod>F20</test:diagnosiscod>
                                <test:description>SCHIZOPHRENIA</test:description>
                            </test:diagnosis>
                            <test:procedure>
                                <test:procedure>
                                    <test:description>HOSPITAL</test:description>
                                </test:procedure>
                                <test:amount>1</test:amount>
                            </test:procedure>
                    </test:proceduresummary>
                </test:billing>
    </test:house>
</test:TASS>

Example of all my files in the directory that should be imported ("D:/"):
20215_ABFF20.xml
35700_38HY9R.xml
38597_40YY9J.xml
99853_99PP1Z.xml
115341_663QQP.xml

The first step I tried was to identify all the files in the directory (“D: /”), I did this:
require(tidyverse)
require(xml2)
setwd("D:/")
files <- list.files(pattern = ".xml$")

How would I import and transform all my xml files into dataframe in one go? (Suppose the files have the same structure.)

A:

Simply generalize your process of one XML into a user-defined method, then build a list of data frames with lapply, to finally stack all data frames at the end.
# USER-DEFINED METHOD
proc_xml <- function(xml_file) {
  page <- read_xml(xml_file)
  ns <- xml_find_all(page, ".//test:billing")
  billing <- jsonlite::fromJSON(jsonlite::toJSON(xml2::as_list(ns)))

  return(billing)
}

# BUILD LIST OF BILLING DATA FRAMES
files <- list.files(pattern = ".xml")
df_list <- lapply(files, proc_xml)

# CONCATENATE INTO ONE MASTER DATA FRAME
final_df <- dplyr::bind_rows(df_list)

