Generating XML Documents Using PHP


The creation or saving of XML data stored in files on disk is the final topic for this chapter. Using the skills that you have already acquired in this chapter or by consulting other chapters in this book, you know how to process World Wide Web forms or query MySQL databases using PHP.

In most cases, the act of creating XML data from variable data that you have already acquired using PHP is as simple as taking one of two steps:

  • If your data is stored in a series of strings or other common objects, write out XML elements and data as needed to a disk file using standard PHP output functions, the same as you would when storing text data to any other type of file.

  • If your data is stored in a specialized object designed for XML manipulationsuch as a DomDocument (DOM extension) or SimpleXMLElement (SimpleXML extension) objectcall the extension's unique function or property for writing the object tree out to an XML file on disk.

Functions and Properties for Storing XML Objects as Files

If you have altered data stored in XML objects managed by PHP extensions, such as the DOM or SimpleXML extensions, and want to write these altered XML trees to text-based XML files, you'll need to use one of the functions or properties shown in Table 9.11 to perform the conversion or output.

Table 9.11. Functions and Properties for Saving XML Data

Function or Property

Extension

Notes on Usage

$object->asXML()

SimpleXML

Returns a well-formatted string containing the entire XML document stored in SimpleXMLElement $object; this can then be saved to a text file.

$object->save()

DOM

Writes the DOM XML tree contained in DomDocument $object out to a text file, whose name is given by a passed string.

$object->saveXML()

DOM

Returns a well-formatted string containing the entire XML document stored in DomDocument $object; this can then be saved to a text file.

$object->saveHTML()

DOM

Returns a well-formatted string containing the entire XML document stored in DomDocument $object using HTML-style formatting; this can then be saved to a text file.

$object->saveHTMLFile()

DOM

Writes the DOM XML tree contained in DomDocument $object out using HTML-style formatting to a text file, whose name is given by a passed string.

$object->dump_file()

DOM XML

Writes the DOM XML tree contained in DomDocument $object out to a text file, whose name is given by a passed string.

$object->dump_mem()

DOM XML

Returns a well-formatted string containing the entire XML document stored in DomDocument $object; this can then be saved to a text file.




PHP 5 Unleashed
PHP 5 Unleashed
ISBN: 067232511X
EAN: 2147483647
Year: 2004
Pages: 257

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net