This paper describes the XML operation classes implemented by PHP. Share to everyone for your reference, as follows:
<?php/* Use method: $test =new XML (), $test->new_xml (' Test.xml '); $test->root (' document '); $test->append_root_ Node (' book '), $test->append_child_node (' author ', ' Linage '), $test->append_child_node (' page '), $test- Append_child_node (' Money ', ' RMB $ '), $test->append_root_node_end (); $test->append_root_node (' book ', ' Name ', ' The "Web" Servers '); $test->append_child_node (' A U T Ho "R ', ' Li n a G e '); $test->append_child_node (' page ', 100); $ Test->append_child_node (' Money ', ' RMB $ '), $test->append_root_node_end (), $test->display (), $test Save (); generated XML result: <?xml version= "1.0" encoding= "Utf-8"?>
linage
RMB
li n A G e
RMB
*/class Xml{var $version, var $encoding, var $start, var $end, var $filename, var $xml _document;var $root _start;var $root _ End;var $rss _start;var $rss _end;function xml ($ver = ' 1.0 ', $encoding = ' GB2312 ') {$this->version= ' <?xml version=/' {$ver}/"encoding=/" {$encoding}/"standal/"?> "; $this->rss_start= "
"; $this->rss_end= "
";} function New_xml ($filename) {$this->filename= $filename; return true;} function root ($element) {$element = $this->filter ($element), if (Isset ($this->start) and Isset ($this->end)) { Exit ("Error:only one Top level element was allowed in an XML document./r/n"); }else{$this->start= "< $element >"; $this->end= "
"; $this->xml_document= $this->version. " /n ". $this->rss_start." /n ". $this->start." /n "; return true; }}function Append_root_node ($title, $property =null, $pro _val=null) {$title = $this->filter ($title); $property =$ This->filter ($property); $pro _val= $this->filter ($pro _val); $property!=null $pro _str= "$property =/" $pro _val/"": $property =null; $c/n "; return true;} function Append_child_node ($title = ' undefined ', $c/n$contents/n ']]-->
/n "; }else{$c/n "; return true;} function display () {header ("Content-type:text/xml"), $xml = $this->xml_document. $this->end. " /n ". $this->rss_end; Echo $xml; return true;} function filter ($sring, $replace _null=true) {$filter []= ' ""; $filter []= "//"; $filter []= "/n"; $filter []= "/R"; $filter [] = "/T"; $replace _null===true $filter []= "": $replace _null=false; foreach ($filter as $val) {$sring =str_replace ($val, ', $sring);} return $sring;} function encode () {//you can add the convert encode function here or add other class to do that}function save () {$this-> ; xml_document= $this->xml_document. $this->end. " /n ". $this->rss_end; $handle =fopen ($this->filename, ' wb+ '); $result =fwrite ($handle, $this->xml_document); Fclose ($handle); if ($result) {return true;} else{echo "Error:can ' t write to files,maybe the access Denied.try to chmod 777 the directory?"; return false;}}
Read more about PHP operations XML related content readers can view this site: "PHP operation tips FOR XML file Summary"
I hope this article is helpful to you in PHP programming.
The above describes the PHP implementation of the XML operation class, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.