Tutorials to .com

Tutorials to .com » Php » Xml » Xml file generation lightweight category

Xml file generation lightweight category

Print View , by: iSee ,Total views: 4 ,Word Count: 1033 ,Date: Thu, 16 Apr 2009 Time: 1:54 AM

php code :----------------------------------------------- ---------------------------------
<?
/ **
* xml document generation lightweight category (non-dom)
* Author: q3boy <q3boy@sina.com>
* Version: v0.1 aplha
* Update: 2003/9/8
* Support Element / CDATA / Declare / attribute / Comment, the option to include line breaks and indentation
* /
class xml (

/ ** Element name * /
var $ name;
/ ** Element values * /
var $ value;
/ ** Element types * /
var $ type;
/ ** Element attributes * /
var $ attrib;
/ ** xml declaration * /
var $ declare;
/ ** Whether the indentation line * /
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 the indentation line * /
function setSpace ($ space) (
$ this-> space = $ space;
)

/ ** Set the element attributes * /
function setAttrib ($ name, $ value ='') (
if (is_array ($ name)) (
$ this-> attrib = array_merge ($ this-> attrib, $ name);
) else (
$ this-> attrib [$ name] = $ value;
)
)

/ ** Add a 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 the 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 the 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 a 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 / indentation * /
if ($ this-> space) (
$ tab = str_repeat ( "", $ layer);
$ tab1 = str_repeat ( "", $ layer +1);
$ br = "\ n";
)
/ * 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 xml document * /
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 ('>'=>'<','<'=>'>','&'=>'&','"'=>'"' ,"'"=>'''));
) elseif ($ type == 'CDATA') (
return '<! [CDATA ['. $ br.str_replace (']]>',']]> ', $ content). $ br .']]>'; [AutoPage]
)
)
)


/ * Example * /

/ * Initialize the object * /
$ xml = new xml ( 'test');

/ * Allow output line / 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');// Notes
/ * 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');
?>


Php XML Application Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.