Example
Build a simple XML document
Example that uses the returnResult option to directly return the serialized XML document in the serialize() method.
In this example look at theses 3 lines.
<?php
$serializer = &new XML_Serializer($options);
$foo = PEAR::raiseError('Just a test', 1234);
$result = $serializer->serialize($foo);
?>
<?php
error_reporting(E_ALL);
require_once 'XML/Serializer.php';
$options = array(
XML_SERIALIZER_OPTION_INDENT => ' ',
XML_SERIALIZER_OPTION_RETURN_RESULT => true
);
$serializer = &new XML_Serializer($options);
$foo = PEAR::raiseError('Just a test', 1234);
$result = $serializer->serialize($foo);
echo '<pre>';
echo htmlspecialchars($result);
echo '</pre>';
?>
And this is the result
<pear_error> <error_message_prefix /> <mode>1</mode> <level>1024</level> <code>1234</code> <message>Just a test</message> <userinfo /> <backtrace> <XML_Serializer_Tag> <file>pathToMypear\PEAR.php</file> <line>566</line> <function>pear_error</function> <class>pear_error</class> <type>-></type> <args> <XML_Serializer_Tag>Just a test</XML_Serializer_Tag> <XML_Serializer_Tag>1234</XML_Serializer_Tag> <XML_Serializer_Tag>1</XML_Serializer_Tag> <XML_Serializer_Tag>1024</XML_Serializer_Tag> <XML_Serializer_Tag /> </args> </XML_Serializer_Tag> <XML_Serializer_Tag> <file>pathToMyDocRoot\cvs.php.net\pear\xml_serializer\examples\serializeandreturn.php</file> <line>19</line> <function>raiseerror</function> <class>pear</class> <type>::</type> <args> <XML_Serializer_Tag>Just a test</XML_Serializer_Tag> <XML_Serializer_Tag>1234</XML_Serializer_Tag> </args> </XML_Serializer_Tag> </backtrace> <callback /> </pear_error>
You can find the last version of this source code in the package : serializeAndReturn.php
Build a RDF document
This example shows how to create an RDF document with a few lines of code. This can also be done with mode => simplexml.
In this example look at theses 3 lines.
<?php
$serializer = new XML_Serializer($options);
$result = $serializer->serialize($rdf);
echo htmlentities($serializer->getSerializedData());
?>
<?php
/**
* @see serializeIndexedArray.php
*/
error_reporting(E_ALL);
require_once 'XML/Serializer.php';
$options = array(
"indent" => " ",
"linebreak" => "\n",
"typeHints" => false,
"addDecl" => true,
"encoding" => "UTF-8",
"rootName" => "rdf:RDF",
"rootAttributes" => array("version" => "0.91"),
"defaultTagName" => "item",
"attributesArray" => "_attributes"
);
$serializer = new XML_Serializer($options);
$rdf = array(
"channel" => array(
"title" => "Example RDF channel",
"link" => "http://www.php-tools.de",
"image" => array(
"title" => "Example image",
"url" => "http://www.php-tools.de/image.gif",
"link" => "http://www.php-tools.de"
),
"_attributes" => array( "rdf:about" => "http://example.com/foobar.html" ),
array(
"title" => "Example item",
"link" => "http://example.com",
"_attributes" => array( "rdf:about" => "http://example.com/foobar.html" )
),
array(
"title" => "Another item",
"link" => "http://example.com",
"_attributes" => array( "rdf:about" => "http://example.com/foobar.html" )
),
array(
"title" => "I think you get it...",
"link" => "http://example.com",
"_attributes" => array( "rdf:about" => "http://example.com/foobar.html" )
)
)
);
$result = $serializer->serialize($rdf);
if( $result === true ) {
echo "<pre>";
echo htmlentities($serializer->getSerializedData());
echo "</pre>";
}
?>
And this is the result
<?xml version="1.0" encoding="UTF-8"?> <rdf:RDF version="0.91"> <channel rdf:about="http://example.com/foobar.html"> <title>Example RDF channel</title> <link>http://www.php-tools.de</link> <image> <title>Example image</title> <url>http://www.php-tools.de/image.gif</url> <link>http://www.php-tools.de</link> </image> <item rdf:about="http://example.com/foobar.html"> <title>Example item</title> <link>http://example.com</link> </item> <item rdf:about="http://example.com/foobar.html"> <title>Another item</title> <link>http://example.com</link> </item> <item rdf:about="http://example.com/foobar.html"> <title>I think you get it...</title> <link>http://example.com</link> </item> </channel> </rdf:RDF>
You can find the last version of this source code in the package : serializeRDF.php
Note : if you search how to parse (read) an RDF/RSS document, look at XML_RSS package.
Build a XML document with a DTD
This example shows how to add a DocType Declaration to the XML document
In this example look at theses 3 lines.
<?php
$serializer = new XML_Serializer($options);
$result = $serializer->serialize($rdf);
echo htmlentities($serializer->getSerializedData());
?>
<?php
error_reporting(E_ALL);
require_once 'XML/Serializer.php';
$options = array(
"indent" => " ",
"linebreak" => "\n",
"addDecl" => true,
"addDoctype" => true,
"doctype" => array(
'uri' => 'http://pear.php.net/dtd/package-1.0',
'id' => '-//PHP//PEAR/DTD PACKAGE 0.1'
)
);
$serializer = new XML_Serializer($options);
$foo = PEAR::raiseError("Just a test", 1234);
$result = $serializer->serialize($foo);
if( $result === true ) {
echo '<pre>';
echo htmlentities($serializer->getSerializedData());
echo '</pre>';
}
?>
And this is the result
<?xml version="1.0"?> <!DOCTYPE pear_error PUBLIC "-//PHP//PEAR/DTD PACKAGE 0.1" "http://pear.php.net/dtd/package-1.0"> <pear_error> <error_message_prefix /> <mode>1</mode> <level>1024</level> <code>1234</code> <message>Just a test</message> <userinfo /> <backtrace> <XML_Serializer_Tag> <file>pathToMyPear\PEAR.php</file> <line>566</line> <function>pear_error</function> <class>pear_error</class> <type>-></type> <args> <XML_Serializer_Tag>Just a test</XML_Serializer_Tag> <XML_Serializer_Tag>1234</XML_Serializer_Tag> <XML_Serializer_Tag>1</XML_Serializer_Tag> <XML_Serializer_Tag>1024</XML_Serializer_Tag> <XML_Serializer_Tag /> </args> </XML_Serializer_Tag> <XML_Serializer_Tag> <file>pathToMyDocumentRoot\cvs.php.net\pear\xml_serializer\examples\serializewithdtd.php</file> <line>24</line> <function>raiseerror</function> <class>pear</class> <type>::</type> <args> <XML_Serializer_Tag>Just a test</XML_Serializer_Tag> <XML_Serializer_Tag>1234</XML_Serializer_Tag> </args> </XML_Serializer_Tag> </backtrace> <callback /> </pear_error>
You can find the last version of this source code in the package : serializeWithDTD.php