Lightweight XML document generation class
<?
/**
* Lightweight XML document generation class (non-DOM)
* Author: q3boy <q3boy@sina.com>
* Version: v0.1 aplha
* Update: 2003/9/8
* Supports element/CDATA/declare/attribute/comment. You can choose whether to include line breaks and indentation.
*/
Class XML {
/** Element name */
VaR $ name;
/** Element value */
VaR $ value;
/** Element type */
VaR $ type;
/** Element attributes */
VaR $ attrib;
/** XML Declaration */
VaR $ declare;
/** Whether to indent line feed */
VaR $ space;
/** Constructor */
Function XML ($ name = '', $ value = ''){
$ This-> name = $ name;
$ This-> value = $ value;
$ This-> declare = array ();
$ This-> settypes ('element ');
$ This-> setattrib (Array ());
$ This-> setspace (false );
}
/** Set the element type */
Function settypes ($ type ){
$ This-> type = $ type;
}
/** Set whether to indent line feed */
Function setspace ($ space ){
$ This-> Space = $ space;
}
/** Set element attributes */
Function setattrib ($ name, $ value = ''){
If (is_array ($ name )){
$ This-> attrib = array_merge ($ this-> attrib, $ name );
} Else {
$ This-> attrib [$ name] = $ value;
}
}
/** Add sub-elements */
Function & addelement ($ name = '', $ value = ''){
If (! Is_array ($ this-> value )){
$ This-> value = array ();
}
$ Xml = New XML ($ name, $ value );
$ XML-> setspace ($ this-> space );
$ This-> value [] = & $ XML;
Return $ this-> value [sizeof ($ this-> value)-1];
}
/** Add CDATA data */
Function & addcdata ($ name = '', $ value = ''){
If (! Is_array ($ this-> value )){
$ This-> value = array ();
}
$ Xml = New XML ($ name, $ value );
$ XML-> setspace ($ this-> space );
$ XML-> settypes ('cdata ');
$ This-> value [] = & $ XML;
Return $ this-> value [sizeof ($ this-> value)-1];
}
/** Add XML Declaration */
Function & adddeclare ($ name = '', $ value = ''){
If (! Is_array ($ this-> declare )){
$ This-> value = array ();
}
$ Xml = New XML ($ name, $ value );
$ XML-> setspace ($ this-> space );
$ XML-> settypes ('desc ');
$ This-> declare [] = & $ XML;
Return $ this-> declare [sizeof ($ this-> value)-1];
}
/** Add Comment text */
Function & addcomment ($ content = ''){
If (! Is_array ($ this-> value )){
$ This-> value = array ();
}
$ Xml = New XML ($ content );
$ XML-> setspace ($ this-> space );
$ XML-> settypes ('comment ');
$ This-> value [] = & $ XML;
Return $ this-> value [sizeof ($ this-> value)-1];
}
/** Return XML text stream */
Function tostring ($ ITM = '', $ layer = 0 ){
If (! Is_object ($ ITM) $ ITM = & $ this;
/* Line feed/indent */
If ($ this-> space ){
$ Tab = str_repeat ("", $ layer );
$ Tab1 = str_repeat ("", $ layer + 1 );
$ BR ="
";
}
/* XML Declaration */
For ($ I = 0; $ I <sizeof ($ ITM-> declare); $ I ++ ){
$ Out = "<? ". $ ITM-> declare [$ I]-> name;
Foreach ($ ITM-> declare [$ I]-> attrib as $ key => $ Val ){
$ Out. = "$ key =" ". $ this-> encode ($ Val ).""";
}
$ Out. = "?> $ Br ";
}
/* Document Tree */
Switch ($ ITM-> type ){
Case 'cdata ':
Case 'element ':
$ Out. = $ tab. '<'. $ ITM-> name;
Foreach ($ ITM-> attrib as $ key => $ Val ){
$ Out. = "$ key =" ". $ this-> encode ($ Val ).""";
}
If (is_array ($ ITM-> value )){
$ Out. = '>'. $ BR;
For ($ I = 0; $ I <sizeof ($ ITM-> value); $ I ++ ){
$ Out. = $ this-> tostring (& $ ITM-> value [$ I], $ layer + 1 );
}
$ Out. = $ tab. '</'. $ ITM-> name. '>'. $ BR;
} Elseif ($ ITM-> value! = ''){
$ Out. = '> '. $ BR. $ this-> encode ($ ITM-> value, $ ITM-> type, $ tab1, $ BR ). $ BR. $ tab. '</'. $ ITM-> name. '> '. $ BR;
} Else {
$ Out. = '/>'. $ BR;
}
Break;
Case 'comment ':
$ Out. = '<! -- '. $ Br. $ ITM-> name. $ Br.' --> '. $ BR;
Break;
}
Return $ out;
}
/** Generate an XML file */
Function tofile ($ file ){
$ Fp = fopen ($ file, 'w ');
Fwrite ($ FP, trim ($ this-> tostring ()));
Fclose ($ FP );
}
/** Entity reference conversion */
Function encode ($ content, $ type = 'element', $ tab1 = '', $ BR = ''){
If ($ type = 'element '){
Return $ tab1.strtr ($ content, array ('>' => '& lt;', '<' => '& gt;', '&' => '& amp; ',' "'=>' & quot; ',"' "=> '& apos ;'));
} Elseif ($ type = 'cdata '){
Return '<! [CDATA ['. $ Br. str_replace (']> ',']> ', $ content). $ Br.']> ';
}
}
}
/* Example */
/* Object initialization */
$ Xml = New XML ('test ');
/* Allow output line breaks/indentation */
$ XML-> setspace (true );
/* Set the XML Declaration */
$ D = & $ XML-> adddeclare ('xml ');
$ D-> setattrib ("version", "1.0 ");
/* Set the XML document tree */
$ Xml1 = & $ XML-> addelement ('test1', 'test1-1 ');
$ Xml1-> addelement ('test2', 'test2-1 ');
$ X2 = & $ xml1-> addelement ('test3', 'test2-2 ');
$ X2-> setattrib ("ASD", "1 & 23 <> 4 '" 23 ");
$ Xml1-> addelement ('test4', 'test2-3 ');
$ XML-> addelement ('test455', 'taadsfa <> fdsadest2-3 ');
$ XML-> addcomment ('adsfadsf '); // comment
/* CDATA data */
$ XML-> addcdata ('cdname', 'dflkgmsglsd
F] GL
Sdgl
Asgl
SF "& ldgsldkfg]>
SLDF
Gsdfgsd? FG> S <DG> S? D <fgsd] fglsg> ');
$ X1 = & $ XML-> addelement ('test455 ');
$ X1-> setattrib ("ASD", 123423 );
$ XML-> setattrib (Array ("ASD" => 123, 'sdfgdfg' => 2341 ));
$ XML-> setattrib ("ASD", 123423 );
/* Output file */
$ XML-> tofile ('aaa. xml ');
?>