Examples for 'XML::saveXML'


Output internal XML Tree

Aliases: saveXML saveXML.XMLInternalDocument saveXML.XMLInternalDOM saveXML.XMLInternalNode saveXML.XMLNode saveXML.XMLOutputStream coerce,XMLInternalDocument,character-method coerce,XMLInternalDOM,character-method coerce,XMLInternalNode,character-method saveXML,XMLFlatTree-method saveXML,XMLInternalDocument-method saveXML,XMLInternalDOM-method saveXML,XMLInternalNode-method saveXML,XMLNode-method saveXML,XMLOutputStream-method saveXML,HTMLInternalDocument-method

Keywords: IO file

### ** Examples


 b = newXMLNode("bob")
 saveXML(b)
[1] "<bob/>"
 f = tempfile()
 saveXML(b, f)
[1] "/tmp/Rtmpul02DQ/filebc45e3fde6795"
 doc = xmlInternalTreeParse(f)
 saveXML(doc)
[1] "<?xml version=\"1.0\"?>\n<bob/>\n"
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=file.path(tempdir(), "out.xml"))
[1] "/tmp/Rtmpul02DQ/out.xml"
# Work with entities

 f = system.file("exampleData", "test1.xml", package = "XML")
 doc = xmlRoot(xmlTreeParse(f))
 outFile = tempfile()
 saveXML(doc, outFile)
[1] "/tmp/Rtmpul02DQ/filebc45e6b38b08a"
 alt = xmlRoot(xmlTreeParse(outFile))
 if(! identical(doc, alt) )
  stop("Problems handling entities!")

 con = textConnection("test1.xml", "w")
 saveXML(doc, con)
A connection with                            
description "test1.xml"     
class       "textConnection"
mode        "w"             
text        "text"          
opened      "opened"        
can read    "no"            
can write   "yes"           
 close(con)
 alt = get("test1.xml")
 identical(doc, alt)
[1] FALSE
 x = newXMLNode("a", "some text", newXMLNode("c", "sub text"), "more text")

 cat(saveXML(x), "\n")
<a>some text<c>sub text</c>more text</a> 
 cat(as(x, "character"), "\n")
<a>some text<c>sub text</c>more text</a> 
     # Showing the prefix parameter
  doc = newXMLDoc()
  n = newXMLNode("top", doc = doc)
  b = newXMLNode("bar", parent = n)

     # suppress the <?xml ...?>
  saveXML(doc, prefix = character())
[1] "<top>\n  <bar/>\n</top>"
     # put our own comment in
  saveXML(doc, prefix = "<!-- This is an alternative prefix -->")
[1] "<!-- This is an alternative prefix -->\n<top>\n  <bar/>\n</top>"
     # or use a comment node.
  saveXML(doc, prefix = newXMLCommentNode("This is an alternative prefix"))
[1] "<!--This is an alternative prefix-->\n<top>\n  <bar/>\n</top>"

[Package XML version 3.99-0.10 Index]