Learn R Programming

XML (version 1.2-0)

xmlOutputBuffer: XML output streams

Description

These two functions provide different ways to construct XML documents incrementally. They provide a single, common interface for adding and closing tags, and inserting nodes. The buffer version stores the XML representation as a string. The DOM version builds the tree of XML node objects.

Usage

xmlOutputBuffer(dtd=NULL, nameSpace="", buf=NULL,
                 nsURI=NULL, header="")

xmlOutputDOM(tag="doc", attrs = NULL, dtd=NULL, nameSpace=NULL, nsURI=character(0))

Arguments

Value

  • Both of these functions return a list of functions which operate on the XML data in a shared environment.
  • valueget the contents of the XML document as they are currently defined.
  • addTagadd a new element to the document, specifying its name and attributes. This allows the tag to be left open so that new elements will be added as children of it.
  • closeTagclose the currently open tag, indicating that new elements will be added, by default, as siblings of this one.
  • resetdiscard the current contents of the document so that we can start over and free the resources (memory) associated with this document.
  • The following are specific to xmlOutputDOM:
  • addNodeinsert an complete XMLNode object into the currently active (i.e. open) node.
  • currentobtain the path or collection of indices to to the currently active/open node from the root node.

Details

These functions create a closure instance which provides methods or functions that operate on shared data used to represent the contents of the XML document being created and the current state of that creation.

References

http://www.omegahat.org/RSXML, http://www.w3.org/xml

See Also

xmlNode, xmlTextNode, append.xmlNode

Examples

Run this code
con <- xmlOutputDOM()
con$addTag("author", "Duncan Temple Lang")
con$addTag("address",  close=FALSE)
 con$addTag("office", "2C-259")
 con$addTag("street", "Mountain Avenue.")
 con$addTag("phone", close=FALSE)
   con$addTag("area", "908", attrs=c(state="NJ"))
   con$addTag("number", "582-3217")
 con$closeTag() # phone
con$closeTag() # address

con$addTag("section", close=FALSE)
 con$addNode(xmlTextNode("This is some text "))
 con$addTag("a","and a link", attrs=c(href="http://www.omegahat.org"))
 con$addNode(xmlTextNode("and some follow up text"))

 con$addTag("subsection", close=FALSE)
   con$addNode(xmlTextNode("some addtional text "))
   con$addTag("a", attrs=c(href="http://www.omegahat.org"), close=FALSE)
     con$addNode(xmlTextNode("the content of the link"))
   con$closeTag() # a
 con$closeTag() # "subsection"
con$closeTag() # section


 d <- xmlOutputDOM()
 d$addPI("S", "plot(1:10)")
 d$addCData('x <- list(1, a="&");<nx>[[2]]')
 d$addComment("A comment")
 print(d$value())
 print(d$value(), indent = FALSE, tagSeparator = "")</nx>
<keyword>file</keyword>
<keyword>IO</keyword>

Run the code above in your browser using DataLab