Tuesday, September 7th, 2010

PHP, DOM and XML : Part 3 – saveXML

In the document object model (DOM) implementation in PHP, The DOMDocument class has two functions for saving xml files; save() and saveXML(). saveXML() is used to output the contents to a string and save() to output to a URI.

Serialize an xml:

1
$output = $domdoc->saveXML();

The above line would serialize the DOMDocument object into

<main><sub>contents</sub></main>

The saveXML() takes an optional node parameter, which should also be a domdocument object. The resulting xml will be just an output of that parameter element.

1
$output = $domdoc->saveXML($sub);

The output would be

<sub>contents</sub>

using save():

1
$size = $domdoc->save('outputdata.xml);

The above code saves the document to the file outputdata.xml and returns the number of bytes written to the variable $size.
The newline characters, tabs, and spaces are removed in case of saveXML(). we can use formatOutput to save the xml with formatting.

1
2
$domdoc->formatOutput = TRUE;
echo $domdoc->saveXML();
Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

Tags: , ,

Readers Comments (0)




Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.


Latest Additions

Popular User Submitted Links

1


Hosted & Optimized JS Libraries – Cached Commons

1


Multi-File Uploader & Resizer – Agile Uploader

1


Open source online notebook-to-do-list manager

1


3 tier app design in silverlight using azure & RIA services

1


Polymaps – A JS Library For Image & Vector-Tiled Maps

eXTReMe Tracker