Version: @(#) $Id: xmlwriterclass.class,v 1.32 2002/10/17 05:38:11 mlemos Exp $
XML writer
Manuel Lemos (mlemos@acm.org)
Copyright © (C) Manuel Lemos 2001-2002
@(#) $Id: xmlwriterclass.class,v 1.32 2002/10/17 05:38:11 mlemos Exp $
Composing and generating XML documents.
The class has several independent functions that should be used to compose the structure of a XML document. When the structure is fully composed, the write should be used to output the document in the XML format.
Here follows an example of typical use of this class:
<?php /* * First create an object of the class. */ $xml_writer_object=new xml_writer_class; /* * Now, start defining the XML document from the root tag. */ $noattributes=array(); $xml_writer_object->addtag("myxmldocument",$noattributes,"",$root,1); /* * Then define the rest of the document tags and data. */ $xml_writer_object->addtag("name",$noattributes,$root,$toptag,0); $xml_writer_object->adddata("John Doe",$toptag,$path); /* * Tags may have attributes. */ $attributes=array(); $attributes["country"]="us"; $xml_writer_object->addtag("address",$attributes,$root,$toptag,1); /* * Tags and the correspondent data may be added with a single function call. */ $xml_writer_object->adddatatag("street",$noattributes,"Wall Street, 1641",$toptag,$datatag); $xml_writer_object->adddatatag("zip",$noattributes,"NY 72834",$toptag,$datatag); /* * Any time before generating the document you may specify a DTD to let other tools validate it... */ $xml_writer_object->dtdtype="SYSTEM"; $xml_writer_object->dtdurl="myxmldocument.dtd"; /* * ...and a stylesheet for displaying the document in particular way in XML capable browsers. */ $xml_writer_object->stylesheettype="text/xsl"; $xml_writer_object->stylesheet="myxmldocument.xsl"; /* * When you are done with the XML document definition, generate it. */ if($xml_writer_object->write($output)) { /* * If the document was generated successfully, you may not output it. */ Header("Content-Type: text/xml; charset=\"".$xml_writer_object->outputencoding."\""); Header("Content-Length: ".strval(strlen($output))); echo $output; } else { /* * If there was an error, output it as well. */ Header("Content-Type: text/plain"); echo ("Error: ".$xml_writer_object->error); } ?>
string
""
Define the URL for the default stylesheet definition to be used render the XML document.
If you intend that browsers display this document in specific way define the URL of where it is located the stylesheet to be used to render the document. Set this variable to an empty string if you do not intend to specify a stylesheet.
string
"text/xsl"
Stylesheets may be of several types. This variable defines the MIME content type of the stylesheet specified for this document.
Set this variable to MIME designation for the type of stylesheet defined by the variable stylesheet.
"text/css"
string
""
Define which type of document type definition (DTD).
If you intend to specify a DTD, eventually for validation purposes, set this variable to either: "INTERNAL", "SYSTEM" or "PUBLIC". Set this variable to an empty string to not use a DTD. "INTERNAL" mean that the DTD is bein defined inline within the dtddefinition variable. "SYSTEM" or "PUBLIC" means that the DTD is defined in an external resource defined by either dtddefinition or dtdurl.
string
""
Define of the DTD.
For an internal DTD, this variable should contain the actual DTD definition. For an external DTD, it should be of the form "Registration//Organization//Type Label//Definition Language".
Example of an internal DTD definition:
"<!ELEMENT html (head, body)>\n<!ELEMENT head (title?)>\n<!ELEMENT title (#PCDATA)>\n<!ELEMENT body (#PCDATA)>\n"
Example of an external DTD definition:
"-//W3C//DTD XHTML 1.0 Strict//EN"
string
""
Define the URL of an external DTD.
Optionally specify an absolute or relative URL of an external DTD.
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
string
"utf-8"
Define the character set encoding of the output document.
Currently the class supports only the encodings utf-8 or iso-8859-1.
string
"iso-8859-1"
Define the character set encoding of the of the data values passed to the function adddata or the attribute values passed to the function addtag.
Currently the class supports only the encodings utf-8 or iso-8859-1.
string
"\n"
Characters to be used when breaking lines of the output XML document.
Use either "\n", "\r\n" or "\r".
string
" "
Characters to be used when indenting the lines of the output XML document.
Use either one or more spaces or tab characters.
string
"Generated by: http://www.phpclasses.org/xmlwriter \$Revision: 1.32 \$"
Define a text string that is used as a XML comment to identify the program that generated the XML document.
Set this variable to the name of the program that uses this class to generate XML documents. Set to an empty string to avoid generating this comment.
"Generated by: my.e-commerce.com"
string
""
Contains the error message that explains the reason of failure of certain class functions.
Check this variable when a given class function fails.
bool write(
Generate the XML document.
Pass a reference to a string variable that will hold the resulting XML document.
output - Reference to a variable that will hold the resulting XML document.
Success boolean flag. If this flag is 0 then the error variable contains the error message that explains the failure.
bool addtag(
Add a tag to the XML document structure.
Specify the tag definition and the parent XML document element where the tag will be added.
tag - Name of the tag to be added.
attributes - Associative array that defines the list of attributes of the tag, if any. The indexes of the array entries are the names of the attributes and the values array entries are the attribute values defined in encoding type specified by the inputencoding variable.
parent - Path of the parent XML document tag element within each which the new tag is being added. If the new tag is the document root tag, the parent path is "". The path of previously added tags is returned by the path argument of previous calls to this function.
path - Reference to a variable that will hold the path of the XML document tag element that is added by this function.
indent - Flag that determines if the elements to be added within this tag will be outputted in individual indented lines.
Success boolean flag. If this flag is 0 then the error variable contains the error message that explains the failure. This return value may be safely ignored if the function call arguments are correctly defined.
bool adddata(
Add a data part to the XML document structure.
Specify the data text and the parent XML document element where the tag will be added.
data - Data text to be added.
parent - Path of the parent XML document tag element within each which the new data part is being added. The path of previously added tags is returned by the path argument of previous calls to the addtag function.
path - Reference to a variable that will hold the path of the XML document data part element that is added by this function.
Success boolean flag. If this flag is 0 then the error variable contains the error message that explains the failure. This return value may be safely ignored if the function call arguments are correctly defined.
bool adddatatag(
Add a tag with data to the XML document structure.
Specify the tag definition, the text data and the parent XML document element where the tag will be added.
tag - Name of the tag to be added.
attributes - Associative array that defines the list of attributes of the tag, if any. The indexes of the array entries are the names of the attributes and the values array entries are the attribute values defined in encoding type specified by the inputencoding variable.
data - Data text to be added.
parent - Path of the parent XML document tag element within each which the new tag is being added. The path of previously added tags is returned by the path argument of previous calls to the addtag function.
path - Reference to a variable that will hold the path of the XML document tag element that is added by this function.
Success boolean flag. If this flag is 0 then the error variable contains the error message that explains the failure. This return value may be safely ignored if the function call arguments are correctly defined.