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 = "parent1", attributes = NULL, children = list(text = list(name = "text", attributes = NULL, children = list(), namespace = "", namespaceDefinitions = list(), value = c("list(name = &quot;child1&quot;, attributes = NULL, children = list(text = list(name = &quot;text&quot;, attributes = NULL, children = list(), namespace = &quot;&quot;, namespaceDefinitions = list(), value = &quot;Child 1 content&quot;)), namespace = &quot;&quot;, namespaceDefinitions = list())", "list(name = &quot;child2&quot;, attributes = NULL, children = list(text = list(name = &quot;text&quot;, attributes = NULL, children = list(), namespace = &quot;&quot;, namespaceDefinitions = list(), value = &quot;Child 2 content&quot;)), namespace = &quot;&quot;, namespaceDefinitions = list())"\n))), namespace = "", namespaceDefinitions = list())list(name = "parent2", attributes = NULL, children = list(text = list(name = "text", attributes = NULL, children = list(), namespace = "", namespaceDefinitions = list(), value = c("list(name = &quot;child1&quot;, attributes = NULL, children = list(text = list(name = &quot;text&quot;, attributes = NULL, children = list(), namespace = &quot;&quot;, namespaceDefinitions = list(), value = &quot;Child 1 content&quot;)), namespace = &quot;&quot;, namespaceDefinitions = list())", "list(name = &quot;child2&quot;, attributes = NULL, children = list(text = list(name = &quot;text&quot;, attributes = NULL, children = list(), namespace = &quot;&quot;, namespaceDefinitions = list(), value = &quot;Child 2 content&quot;)), namespace = &quot;&quot;, namespaceDefinitions = list())"\n))), namespace = "", namespaceDefinitions = list())</root>"
Created on 2023-09-16 with reprex v2.0.2