saveXML {XML} | R Documentation

## Output internal XML Tree

### Description

Methods for writing the representation of an XML tree to a string or file. Originally this was intended to be used only for DOMs (Document Object Models) stored in internal memory created via `xmlTree`, but methods for `XMLNode`, `XMLInternalNode` and `XMLOutputStream` objects (and others) allow it to be generic for different representations of the XML tree.

Note that the indentation when writing an internal C-based node (XMLInternalNode) may not be as expected if there are text nodes within the node.

Also, not all the parameters are meaningful for all methods. For example, compressing when writing to a string is not supported.

### Usage

    saveXML(doc, file=NULL, compression=0, indent=TRUE, prefix = '<?xml version="1.0"?>\n',
            doctype = NULL, encoding = "")
    ## S3 method for class 'XMLInternalDocument':
    saveXML(doc, file=NULL, compression=0, indent=TRUE, prefix = '<?xml version="1.0"?>\n',
                                doctype = NULL, encoding = "")
    ## S3 method for class 'XMLInternalDOM':
    saveXML(doc, file=NULL, compression=0, indent=TRUE, prefix = '<?xml version="1.0"?>\n',
                           doctype = NULL, encoding = "")
    ## S3 method for class 'XMLNode':
    saveXML(doc, file=NULL, compression=0, indent=TRUE, prefix = '<?xml version="1.0"?>\n',
                     doctype = NULL, encoding = "")
    ## S3 method for class 'XMLOutputStream':
    saveXML(doc, file=NULL, compression=0, indent=TRUE, prefix = '<?xml version="1.0"?>\n',
                             doctype = NULL, encoding = "")

### Arguments

`doc` | the document object representing the XML document.
`file` | the name of the file to which the contents of the XML nodes will be serialized.
`compression` | an integer value between 0 and 9 indicating the level of compression to use when saving the file. Higher values indicate increased compression and hence smaller files at the expense of computational time to do the compression and decompression.
`indent` | a logical value indicating whether to indent the nested nodes when serializing to the stream.
`prefix` | a string that is written to the stream/connection before the XML is output. If this is NULL, it is ignored. This allows us to put the XML introduction/preamble at the beginning of the document while allowing it to be omitted when we are outputting multiple "documents" within a single stream.
`doctype` | an object identifying the elements for the DOCTYPE in the output. This can be a string or an object of class `Doctype`.
`encoding` | a string indicating which encoding style to use. This is currently ignored except in the method in `Sxslt` for saving a document generated by applying an XSL style sheet to an XML document.

### Details

One can create an internal XML tree (or DOM) using `newXMLDoc` and `newXMLNode`. `saveXML` allows one to generate a textual representation of that DOM in human-readable and reusable XML format. `saveXML` is a generic function that allows one to call the rendering operation with either the top-level node of the DOM or of the document object (of class `XMLInternalDocument` that is used to accumulate the nodes and with which the developer adds nodes.

### Value

If `file` is not specified, the result is a character string containing the resulting XML content. If `file` is passed in the call,

### Author(s)

Duncan Temple Lang

### References

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

### See Also

`newXMLDoc` `newXMLNode` `xmlOutputBuffer` `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() # phone
    con$closeTag() # address

    saveXML(con$value(), file="out.xml")

    # Work with entities

     f = 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")

[Package XML version 1.99-0 Index]