parseXMLAndAdd: Parse XML content and add it to a node

View source: R/xmlTree.R

parseXMLAndAddR Documentation

Parse XML content and add it to a node


This function parses the given XML content as a string by putting it inside a top-level node and then returns the document or adds the children to the specified parent. The motivation for this function is when we can use string manipulation to efficiently create the XML content by using vectorized operations in R, but then converting that content into parsed nodes.

Generating XML/HTML content by glueing strings together is a poor approach. It is often convenient, but rarely good general software design. It makes for bad software that is not very extensible and difficult to maintain and enhance. Structure that it is programmatically accessible is much better. The tree approach provides this structure. Using strings is convenient and somewhat appropriate when done atomically for large amounts of highly regular content. But then the results should be converted to the structured tree so that they can be modified and extended. This function facilitates using strings and returning structured content.


parseXMLAndAdd(txt, parent = NULL, top = "tmp", nsDefs = character())



the XML content to parse


an XMLInternalNode to which the top-level nodes in txt will be added as children


the name for the top-level node. If parent is specified, this is used but irrelevant.


a character vector of name = value pairs giving namespace definitions to be added to the top node.


If parent is NULL, the root node of the parsed document is returned. This will be an element whose name is given by top unless the XML content in txt is AsIs or code is empty.

If parent is non-NULL, .


Duncan Temple Lang

See Also

newXMLNode xmlParse addChildren


  long = runif(10000, -122, -80)
  lat = runif(10000, 25, 48)

  txt = sprintf("<Placemark><Point><coordinates>%.3f,%.3f,0</coordinates></Point></Placemark>",
                  long, lat)
  f = newXMLNode("Folder")
  parseXMLAndAdd(txt, f)

## Not run: 
      # this version is much slower as i) we don't vectorize the
      #  creation of the XML nodes, and ii) the parsing of the XML
      # as a string is very fast as it is done in C.
  f = newXMLNode("Folder")
  mapply(function(a, b) {
                                               paste(a, b, "0", collapse = ","))), 
		       parent = f)
         long, lat) 

  o = c("<x>dog</x>", "<omg:x>cat</omg:x>")
  node = parseXMLAndAdd(o, nsDefs  = c("",
                                       omg = ""))

  tt = newXMLNode("myTop")
  node = parseXMLAndAdd(o, tt, nsDefs  = c("",
                                           omg = ""))

## End(Not run)

XML documentation built on Oct. 29, 2022, 1:13 a.m.

Related to parseXMLAndAdd in XML...