Rev 8141 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{saveXML}\alias{saveXML}\alias{saveXML.XMLInternalDocument}\alias{saveXML.XMLInternalDOM}\alias{saveXML.XMLInternalNode}\alias{saveXML.XMLNode}\alias{saveXML.XMLOutputStream}\alias{coerce,XMLInternalDocument,character-method}\alias{coerce,XMLInternalDOM,character-method}\alias{coerce,XMLInternalNode,character-method}\alias{saveXML,XMLFlatTree-method}\alias{saveXML,XMLInternalDocument-method}\alias{saveXML,XMLInternalDOM-method}\alias{saveXML,XMLInternalNode-method}\alias{saveXML,XMLNode-method}\alias{saveXML,XMLOutputStream-method}\alias{saveXML,HTMLInternalDocument-method}\title{Output internal XML Tree}\description{Methods for writing the representation of an XML tree to a string orfile.Originally this was intended to be used only forDOMs (Document Object Models) stored in internal memorycreated via \code{\link{xmlTree}}, but methods for\code{XMLNode}, \code{XMLInternalNode} and \code{XMLOutputStream}objects(and others)allow it to be generic for different representations of theXML tree.Note that the indentation when writing an internal C-based node(XMLInternalNode) may not be as expected if there are text nodeswithin the node.Also, not all the parameters are meaningful for all methods.For example, compressing when writing to a string is notsupported.}\usage{saveXML(doc, file=NULL, compression=0, indent=TRUE, prefix = '<?xml version="1.0"?>\n',doctype = NULL, encoding = getEncoding(doc), ...)\method{saveXML}{XMLInternalDocument}(doc, file=NULL, compression=0, indent=TRUE, prefix = '<?xml version="1.0"?>\n',doctype = NULL, encoding = getEncoding(doc), ...)\method{saveXML}{XMLInternalDOM}(doc, file=NULL, compression=0, indent=TRUE, prefix = '<?xml version="1.0"?>\n',doctype = NULL, encoding = getEncoding(doc), ...)\method{saveXML}{XMLNode}(doc, file=NULL, compression=0, indent=TRUE, prefix = '<?xml version="1.0"?>\n',doctype = NULL, encoding = getEncoding(doc), ...)\method{saveXML}{XMLOutputStream}(doc, file=NULL, compression=0, indent=TRUE, prefix = '<?xml version="1.0"?>\n',doctype = NULL, encoding = getEncoding(doc), ...)}\arguments{\item{doc}{the document object representing the XML document.}\item{file}{the name of the file to which the contents of the XMLnodes will be serialized.}\item{compression}{an integer value between 0 and 9 indicating thelevel of compression to use when saving the file. Higher valuesindicate increased compression and hence smaller filesat the expense of computational time to do the compression and decompression.}\item{indent}{a logical value indicating whether to indentthe nested nodes when serializing to the stream.}\item{prefix}{a string that is written to the stream/connection beforethe XML is output. If this is NULL, it is ignored. This allows us toput the XML introduction/preamble at the beginning of the documentwhile allowing it to be omitted when we are outputting multiple"documents" within a single stream.}\item{doctype}{an object identifying the elements for the DOCTYPE in the output.This can be a string or an object of class \code{Doctype}.}\item{encoding}{a string indicating which encoding style to use. Thisis currently ignored except in the method in \code{Sxslt} for saving adocument generated by applying an XSL style sheet to an XML document.}\item{\dots}{extra parameters for specific methods}}\details{One can create an internal XML tree (or DOM)using \code{\link{newXMLDoc}} and \code{\link{newXMLNode}}.\code{saveXML} allows one to generate a textual representation ofthat DOM in human-readable and reusable XML format.\code{saveXML} is a generic function that allows one to callthe rendering operation with either the top-level nodeof the DOM or of the document object (of class \code{XMLInternalDocument}that is used toaccumulate the nodes and with which the developeradds nodes.}\value{If \code{file} is not specified, the result is a character string containingthe resulting XML content.If \code{file} is passed in the call,}\references{\url{https://www.w3.org/XML/}, \url{https://www.omegahat.net/RSXML/}}\author{Duncan Temple Lang}\seealso{\code{\link{newXMLDoc}}\code{\link{newXMLNode}}\code{\link{xmlOutputBuffer}}\code{\link{xmlOutputDOM}}}\examples{b = newXMLNode("bob")saveXML(b)f = tempfile()saveXML(b, f)doc = xmlInternalTreeParse(f)saveXML(doc)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() # phonecon$closeTag() # addresssaveXML(con$value(), file=file.path(tempdir(), "out.xml"))# Work with entitiesf = system.file("exampleData", "test1.xml", package = "XML")doc = xmlRoot(xmlTreeParse(f))outFile = tempfile()saveXML(doc, outFile)alt = xmlRoot(xmlTreeParse(outFile))if(! identical(doc, alt) )stop("Problems handling entities!")con = textConnection("test1.xml", "w")saveXML(doc, con)close(con)alt = get("test1.xml")identical(doc, alt)x = newXMLNode("a", "some text", newXMLNode("c", "sub text"), "more text")cat(saveXML(x), "\n")cat(as(x, "character"), "\n")# Showing the prefix parameterdoc = newXMLDoc()n = newXMLNode("top", doc = doc)b = newXMLNode("bar", parent = n)# suppress the <?xml ...?>saveXML(doc, prefix = character())# put our own comment insaveXML(doc, prefix = "<!-- This is an alternative prefix -->")# or use a comment node.saveXML(doc, prefix = newXMLCommentNode("This is an alternative prefix"))}\keyword{IO}\keyword{file}