php实现的xml操作类
本文实例讲述了php实现的xml操作类。分享给大家供大家参考,具体如下:
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','35 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','35 RMB'); $test->append_root_node_end(); $test->display(); $test->save(); 生成的xml结果: <document> <book> <author>linage</author> <page>100</page> <money>35 RMB</money> </book> <book name="TheWebServers"> <author>li n a g e</author> <page>100</page> <money>35 RMB</money> </book> </document> */ 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=""; $this->rss_start="<rss version='/"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>"; } 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 is allowed in an XML document./r/n"); }else{ $this->start=""; $this->end="$element>"; $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']]-->$title>/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; } } }
更多关于PHP操作xml相关内容感兴趣的读者可查看本站专题:《PHP针对XML文件操作技巧总结》
希望本文所述对大家PHP程序设计有所帮助。
以上就介绍了php实现的xml操作类,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

华为正在全球推出Watch GT 5和Watch GT 5 Pro智能手表的软件版本5.0.0.100(C00M01)。这两款智能手表最近在欧洲推出,标准型号是该公司最便宜的型号。这和谐

《铁拳》系列总监原田胜宏曾认真尝试过将桑德斯上校带入这款标志性格斗游戏中。在接受 TheGamer 采访时,原田透露,他向日本肯德基提出了这个想法,希望将这位快餐传奇人物纳入其中。

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

2024 年 9 月早些时候,Anker 的 Zolo 140W 充电器被泄露,这是该公司首款带显示屏的壁式充电器,这引起了轰动。现在,小李TV在YouTube上发布的新开箱视频让我们亲眼目睹了这款hi

小米即将在中国推出米家石墨烯油汀取暖器。该公司最近在其优品平台上成功举办了一次智能家居产品众筹活动。根据页面显示,该设备已经开始发货至

特斯拉正在推出最新的全自动驾驶(监督)版本 12.5.5,并最终带来了承诺的 Cybertruck FSD 选项,距离皮卡上市十个月后,该功能包含在基础系列的装饰价格中。 F

Garmin 将于本月底为其最新的高端智能手表提供一组新的稳定更新。回顾一下,该公司发布了系统软件 11.64,以解决 Enduro 3、Fenix E 和 Fenix 8 的高电池消耗问题(亚马逊售价 1,099.99 美元)。

三星期待已久的“特别版”可折叠手机的推出又迎来了另一个转折。最近几周,有关所谓 Galaxy Z Fold 特别版的传言相当安静。相反,焦点已转移到 Galaxy S25 系列,包括
