php实现的xml操作类
本文实例讲述了php实现的xml操作类。分享给大家供大家参考,具体如下:
<?php
/*
使用方法:
$test=newxml();
$test->new_xml('test.xml');
$test->root('document');
$test->append_root_node('book');
$test->append_child_node('author','linage');
$test->append_child_node('page',100);
$test->append_child_node('money','35RMB');
$test->append_root_node_end();
$test->append_root_node('book','name','The"Web"Servers');
$test->append_child_node('autho"r','linage');
$test->append_child_node('page',100);
$test->append_child_node('money','35RMB');
$test->append_root_node_end();
$test->display();
$test->save();
生成的xml结果:
<?xmlversion="1.0"encoding="utf-8"?>
<document>
<book>
<author>linage</author>
<page>100</page>
<money>35RMB</money>
</book>
<bookname="TheWebServers">
<author>linage</author>
<page>100</page>
<money>35RMB</money>
</book>
</document>
*/
classxml{
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;
functionxml($ver='1.0',$encoding='GB2312'){
$this->version="<?xmlversion=/"{$ver}/"encoding=/"{$encoding}/"standalone=/"yes/"?>";
$this->rss_start="<rssversion=/"2.0/"xmlns:domxml=/"[url]http://xml.666life.com/rss/1.0[/url]/"xmlns:geo=/"[url]http://www.w3.org/2003/01/geo/wgs84_pos#[/url]/">";
$this->rss_end="</rss>";
}
functionnew_xml($filename){
$this->filename=$filename;
returntrue;
}
functionroot($element){
$element=$this->filter($element);
if(isset($this->start)andisset($this->end)){
exit("error:OnlyonetoplevelelementisallowedinanXMLdocument./r/n");
}else{
$this->start="<$element>";
$this->end="</$element>";
$this->xml_document=$this->version."/n".$this->rss_start."/n".$this->start."/n";
returntrue;
}
}
functionappend_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;
$contents="<{$title}{$pro_str}>/n";
$this->xml_document.=$contents;
$this->root_end="</$title>";
returntrue;
}
functionappend_root_node_end(){
$this->xml_document.=$this->root_end."/n";
returntrue;
}
functionappend_child_node($title='undefined',$contents='undefined',$property=null,$pro_val=null,$cddate=false){
isset($property)?$pro_str="$property=/"$pro_val/"":$property=null;
$title=$this->filter($title);
$contents=$this->filter($contents,false);
$property=$this->filter($property);
$pro_val=$this->filter($pro_val);
$cddate===false?$cddate=false:$cddate=true;
if($cddate){
$contents="<{$title}{$pro_str}><!--[CDATA['/n$contents/n']]--></$title>/n";
}else{
$contents="<{$title}{$pro_str}>$contents</$title>";
}
$this->xml_document.=$contents."/n";
returntrue;
}
functiondisplay(){
header("Content-type:text/xml");
$xml=$this->xml_document.$this->end."/n".$this->rss_end;
echo$xml;
//returntrue;
}
functionfilter($sring,$replace_null=true){
$filter[]='"';
$filter[]="//";
$filter[]="/n";
$filter[]="/r";
$filter[]="/t";
$replace_null===true?$filter[]="":$replace_null=false;
foreach($filteras$val){
$sring=str_replace($val,'',$sring);
}
return$sring;
}
functionencode(){
//youcanaddtheconvertencodefunctionhereoraddotherclasstodothat
}
functionsave(){
$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){
returntrue;
}else{
echo"error:can'twritetofiles,maybetheaccessdenied.trytochmod777thedirectory?";
returnfalse;
}
}
}
更多关于PHP操作xml相关内容感兴趣的读者可查看本站专题:《PHP针对XML文件操作技巧总结》
希望本文所述对大家PHP程序设计有所帮助。