How to combine multiple XML files into a single one and export

Dear R users,

I am new to this forum and would like to know whether it is possible to combine multiple XML files and append them to be exported as a new one.

I am using a loop to create different XML files to be uploaded to the Moodle quiz bank.

for (group in group_name){
for(item in item_name){
for(stage in stage_name){
myexam <-list( 'Probability.Rmd')

exams2moodle(myexam,
dir = here::here("Final Exam/S2_2023"),
edir = here::here("Final Exam/S2_2023"), name = paste0(group, item, stage),
n = 1)

}
}
}

The result is 54 different XML files, but Moodle only accepts a single file upload, which is a pain.

I can read them all to R again, but I do not know the functions to combine them and export them as a single XML. Can someone please offer some insights? I'm not very familiar with XML, only a basic R user and I use Rexams package for teaching.

all.files <- list.files(pattern="\.xml", path='Final Exam/S2_2023', full.names=TRUE)

I appreciate any help you could offer.

Kind regards
Jaslene

If the XML objects have identical structures

library(XML)
# Create the first XML object
xml1 <- xmlNode("parent1")
xml1_child1 <- xmlNode("child1", "Child 1 content")
xml1_child2 <- xmlNode("child2", "Child 2 content")
xml1 <- addChildren(xml1, list(xml1_child1, xml1_child2))

# Create the second XML object
xml2 <- xmlNode("parent2")
xml2_child1 <- xmlNode("child1", "Child 1 content")
xml2_child2 <- xmlNode("child2", "Child 2 content")
xml2 <- addChildren(xml2, list(xml2_child1, xml2_child2))

# Combine the XML objects
combined_xml <- xmlNode("root")
combined_xml <- addChildren(combined_xml, list(xml1, xml2))

saveXML(combined_xml)
#> [1] "<?xml version=\"1.0\"?>\n<root>list(name = &quot;parent1&quot;, attributes = NULL, children = list(text = list(name = &quot;text&quot;, attributes = NULL, children = list(), namespace = &quot;&quot;, namespaceDefinitions = list(), value = c(&quot;list(name = &amp;quot;child1&amp;quot;, attributes = NULL, children = list(text = list(name = &amp;quot;text&amp;quot;, attributes = NULL, children = list(), namespace = &amp;quot;&amp;quot;, namespaceDefinitions = list(), value = &amp;quot;Child 1 content&amp;quot;)), namespace = &amp;quot;&amp;quot;, namespaceDefinitions = list())&quot;, &quot;list(name = &amp;quot;child2&amp;quot;, attributes = NULL, children = list(text = list(name = &amp;quot;text&amp;quot;, attributes = NULL, children = list(), namespace = &amp;quot;&amp;quot;, namespaceDefinitions = list(), value = &amp;quot;Child 2 content&amp;quot;)), namespace = &amp;quot;&amp;quot;, namespaceDefinitions = list())&quot;\n))), namespace = &quot;&quot;, namespaceDefinitions = list())list(name = &quot;parent2&quot;, attributes = NULL, children = list(text = list(name = &quot;text&quot;, attributes = NULL, children = list(), namespace = &quot;&quot;, namespaceDefinitions = list(), value = c(&quot;list(name = &amp;quot;child1&amp;quot;, attributes = NULL, children = list(text = list(name = &amp;quot;text&amp;quot;, attributes = NULL, children = list(), namespace = &amp;quot;&amp;quot;, namespaceDefinitions = list(), value = &amp;quot;Child 1 content&amp;quot;)), namespace = &amp;quot;&amp;quot;, namespaceDefinitions = list())&quot;, &quot;list(name = &amp;quot;child2&amp;quot;, attributes = NULL, children = list(text = list(name = &amp;quot;text&amp;quot;, attributes = NULL, children = list(), namespace = &amp;quot;&amp;quot;, namespaceDefinitions = list(), value = &amp;quot;Child 2 content&amp;quot;)), namespace = &amp;quot;&amp;quot;, namespaceDefinitions = list())&quot;\n))), namespace = &quot;&quot;, namespaceDefinitions = list())</root>"

Created on 2023-09-16 with reprex v2.0.2

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.