php

位置:IT落伍者 >> php >> 浏览文章

使用PHP 5.0 轻松解析XML文档(1)


发布日期:2018年05月23日
 
使用PHP 5.0 轻松解析XML文档(1)

用sax方式的时候要自己构建个函数而且要直接用这三的函数来返回数据 要求较强的逻辑 在处理不同结构的xml的时候 还要重新进行构造这三个函数麻烦!

用dom方式倒是好些但是他把每个节点都看作是一个node操作起来要写好多的代码 麻烦!

网上有好多的开源的xml解析的类库 以前看过几个但是心里总是觉得不踏实感觉总是跟在别人的屁股后面

这几天在搞java 挺累的所以决定换换脑袋写点php代码为了防止以后xml解析过程再令我犯难就花了一天的时间写了下面一个xml解析的类于是就有了下面的东西

实现方式是通过包装sax方式的解析结果来实现的 总的来说对于我个人来说挺实用的性能也还可以基本上可以完成大多数的处理要求

功能:

对基本的xml文件的节点进行 查询 / 添加 / 修改 / 删除 工作

导出xml文件的所有数据到一个数组里面

整个设计采用了oo方式在操作结果集的时候 使用方法类似于dom

缺点:

每个节点最好都带有一个id(看后面的例子) 每个节点名字=节点的标签_节点的id如果这个id值没有设置程序将自动给他产生一个id这个id就是这个节点在他的上级节点中的位置编号开始

查询某个节点的时候可以通过用|符号连接节点名字来进行这些节点名字都是按顺序写好的上级节点的名字

使用说明

运行下面的例子在执行结果页面上可以看到函数的使用说明

代码是通过php来实现的在php中无法正常运行

由于刚刚写完所以没有整理文档下面的例子演示的只是一部分的功能代码不是很难要是想知道更多的功能可以研究研究源代码

目录结构:

testphp

testxml

xml / SimpleDocumentBasephp

xml / SimpleDocumentNodephp

xml / SimpleDocumentRootphp

xml / SimpleDocumentParserphp

文件:testxml

<?xml version= encoding=GB?><shop> <name>华联</name> <address>北京长安街-号</address> <desc>连锁超市</desc> <cat id=food><goods id=food> <name>food</name> <price></price></goods><goods id=food> <name>food</name> <price></price> <desc creator=hahawen>好东西推荐</desc></goods> </cat> <cat><goods id=tel> <name>tel</name> <price></price></goods> </cat> <cat id=coat><goods id=coat> <name>coat</name> <price></price></goods><goods id=coat> <name>coat</name> <price></price></goods> </cat> <special id=hot><goods> <name>hot</name> <price></price></goods> </special></shop>

文件:testphp

<?php

require_once xml/SimpleDocumentParserphp;require_once xml/SimpleDocumentBasephp;require_once xml/SimpleDocumentRootphp;require_once xml/SimpleDocumentNodephp; $test = new SimpleDocumentParser();$test>parse(testxml);$dom = $test>getSimpleDocument();echo <pre>;echo <hr><font color=red>;echo 下面是通过函数getSaveData()返回的整个xml数据的数组;echo </font><hr>;print_r($dom>getSaveData());echo <hr><font color=red>;echo 下面是通过setValue()函数给给根节点添加信息添加后显示出结果xml文件的内容;echo </font><hr>;$dom>setValue(telphone );echo htmlspecialchars($dom>getSaveXml());echo <hr><font color=red>;echo 下面是通过getNode()函数返回某一个分类下的所有商品的信息;echo </font><hr>;$obj = $dom>getNode(cat_food);$nodeList = $obj>getNode();foreach($nodeList as $node){$data = $node>getValue();echo <font color=red>商品名$data[name]</font><br>;print_R($data);print_R($node>getAttribute());}echo <hr><font color=red>;echo 下面是通过findNodeByPath()函数返回某一商品的信息;echo </font><hr>;$obj = $dom>findNodeByPath(cat_food|goods_food);if(!is_object($obj)){echo 该商品不存在;}else{$data = $obj>getValue();echo <font color=red>商品名$data[name]</font><br>;print_R($data);print_R($obj>getAttribute());} echo <hr><font color=red>;echo 下面是通过setValue()函数给商品\food\添加属性 然后显示添加后的结果;echo </font><hr>;$obj = $dom>findNodeByPath(cat_food|goods_food);$obj>setValue(leaveword array(value=>这个商品不错attrs=>array(author=>hahawen date=>date(Ymd))));echo htmlspecialchars($dom>getSaveXml());echo <hr><font color=red>;echo 下面是通过removeValue()/removeAttribute()函数给商品\food\改变和删除属性 然后显示操作后的结果;echo </font><hr>;$obj = $dom>findNodeByPath(cat_food|goods_food);$obj>setValue(name new food);$obj>removeValue(desc);echo htmlspecialchars($dom>getSaveXml());echo <hr><font color=red>;echo 下面是通过createNode()函数添加商品 然后显示添加后的结果;echo </font><hr>;$obj = $dom>findNodeByPath(cat_food);$newObj = $obj>createNode(goods array(id=>food));$newObj>setValue(name food);$newObj>setValue(price );echo htmlspecialchars($dom>getSaveXml());echo <hr><font color=red>;echo 下面是通过removeNode()函数删除商品 然后显示删除后的结果;echo </font><hr>;$obj = $dom>findNodeByPath(cat_food);$obj>removeNode(goods_food);echo htmlspecialchars($dom>getSaveXml());?>

文件:SimpleDocumentParserphp

<?php

/**

*=========================================================

*

* @author hahawen(大龄青年)

* @since

* @copyrightCopyright (c) NxCoder Group

*

*=========================================================

*/

/**

* class SimpleDocumentParser

* use SAX parse xml file and build SimpleDocumentObject

* all this pachages is work for xml file and method is action as DOM

*

* @package monxml

* @version

*/

class SimpleDocumentParser

{

private $domRootObject = null;

private $currentNO = null;

private $currentName = null;

private $currentValue = null;

private $currentAttribute = null;

public function getSimpleDocument()

{

return $this>domRootObject;

}

public function parse($file)

{

$xmlParser = xml_parser_create();

xml_parser_set_option($xmlParserXML_OPTION_CASE_FOLDING );

xml_parser_set_option($xmlParserXML_OPTION_SKIP_WHITE );

xml_parser_set_option($xmlParser XML_OPTION_TARGET_ENCODING UTF);

xml_set_object($xmlParser $this);

xml_set_element_handler($xmlParser startElement endElement);

xml_set_character_data_handler($xmlParser characterData);

if (!xml_parse($xmlParser file_get_contents($file)))

die(sprintf(XML error: %s at line %d xml_error_string(xml_get_error_code($xmlParser))xml_get_current_line_number($xmlParser)));

xml_parser_free($xmlParser);

}

private function startElement($parser $name $attrs)

{

$this>currentName = $name;

$this>currentAttribute = $attrs;

if($this>currentNO == null)

{

$this>domRootObject = new SimpleDocumentRoot($name);

$this>currentNO = $this>domRootObject;

}

else

{

$this>currentNO = $this>currentNO>createNode($name $attrs);

}

}

private function endElement($parser $name)               

上一篇:PHP程序员一般都忽略了的几点精华

下一篇:Extended CHM PHP 语法手册之 DIY