Lightweight XML Document Generation class
Last Update:2017-02-28
Source: Internet
Author: User
XML PHP Code:--------------------------------------------------------------------------------
?
/**
* Lightweight XML document Generation Class (non-DOM)
* Author:q3boy <q3boy@sina.com>
* version:v0.1 Aplha
* UPDATE:2003/9/8
* Support Element/cdata/declare/attribute/comment, choose whether to include line wrapping 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 the line is indented or not
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 Element type * *
function Settypes ($type) {
$this->type = $type;
}
/** Set whether indent line
function Setspace ($space) {
$this->space = $space;
}
/** Set element Properties */
function Setattrib ($name, $value = ' ") {
if (Is_array ($name)) {
$this->attrib = Array_merge ($this->attrib, $name);
}else {
$this->attrib[$name] = $value;
}
}
/** Add child element * *
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 (' Declare ');
$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];
}
/** returns the XML text stream * *
function toString ($itm = ', $layer =0) {
if (!is_object ($ITM)) $itm = & $this;
/* Line Break/indent * *
if ($this->space) {
$tab = Str_repeat ("", $layer);
$tab 1 = str_repeat ("", $layer + 1);
$BR = "\ n";
}
/* XML declaration * *
For ($i =0 $i <sizeof ($itm->declare); $i + +) {
$out = "A;?". $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, $tab 1, $BR). $br. $tab. ' </'. $itm->name. ' > '. $br;
}else {
$out. = '/> '. $br;
}
Break
Case ' Comment ':
$out. = ' <!--'. $br. $itm->name $br. $BR,-->.
Break
}
return $out;
}
/** Generate XML File * *
function ToFile ($file) {
$fp = fopen ($file, ' w ');
Fwrite ($fp, Trim ($this->tostring ()));
Fclose ($FP);
}
/** entity reference Conversion * *
function encode ($content, $type = ' Element ', $tab 1= ', $br = ') {
if ($type = = ' Element ') {
Return $tab 1.STRTR ($content, Array (' > ' => ' < ', ' < ' => ' > ', ' & ' => ' & ', ' "' => '", "'" => ') ''));
}elseif ($type = = ' CDATA ') {
Return ' <! [cdata['. $br. Str_replace (']]> ', ']] > ', $content). $br. ']]> ';
}
}
}
* Example * *
/* Object Initialization * *
$xml = new XML (' Test ');
/* Allow output line/indent/indentation *
$xml->setspace (TRUE);
/* Set XML declaration * *
$d = & $xml->adddeclare (' xml ');
$d->setattrib ("Version", "1.0");
/* Set XML document Tree * *
$xml 1 = & $xml->addelement (' test1 ', ' test1-1 ');
$xml 1->addelement (' test2 ', ' test2-1 ');
$x 2 = & $xml 1->addelement (' test3 ', ' test2-2 ');
$x 2->setattrib ("ASD", "1&23<>4", "23");
$xml 1->addelement (' test4 ', ' test2-3 ');
$xml->addelement (' test455 ', ' taadsfa<><>fdsadest2-3 ');
$xml->addcomment (' ADSFADSF ');/note
/* CDATA Data * *
$xml->addcdata (' cdname ', ' DFLKGMSGLSD
F]gl
Sdgl
Asgl
SF "&ldgsldkfg]]>
Sldf
Gsdfgsd? Fg>s<dg>s? D<fgsd]fglsg>> ');
$x 1 = & $xml->addelement (' test455 ');
$x 1->setattrib ("ASD", 123423);
$xml->setattrib ("ASD" =>123, ' SDFGDFG ' =>2341));
$xml->setattrib ("ASD", 123423);
/* Output File * *
$xml->tofile (' aaa.xml ');
?>