今天写了一个sitemap创建的类文件,用到了PHP的XML生成类DOMDocument(),其实,这个类非常好用,也非常简单,只是标签继承的逻辑问题总会搞的迷糊,所以,今天写了一个简单的小类,类很小,也很简单,主要的目的是帮助我们理清思路,不再在标签的继承上迷糊。
下面是PHP的XML生成类的源码,请参考:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php class domxml { private $dot; public function __construct($version,$ecode){ $this->dot=new DOMDocument($version,$ecode);//实例化DOM对象 $this->dot->formatOutput=true;//格式化输出设置 } public function createTag($tagName){//创建标签 $tagObj=$this->dot->createElement($tagName); return $tagObj; } public function createAtt($attName){ $attObj=$this->dot->createAttribute($attName); return $attObj; } public function creatContent($str){//创建内容 $contentObj=$this->dot->createTextNode($str); return $contentObj; } public function toTag($sonObj,$parentObj){//标签继承 包括内容继承和标签继承 //子对象,父对象 $parentObj->appendChild($sonObj); } public function bigTag($tagNmae){//设置主标签 $this->dot->appendChild($tagNmae); } public function saveXml($xmlName){//保存文件到路径 $this->dot->save($xmlName); } } ?> |