仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1204|回复: 19
打印 上一主题 下一主题

[学习教程] PHP教程之利用PHP 5.0 轻松解析XML文档(2)

[复制链接]
若天明 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:29:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
不过语法好学,但是怎么用语法来实现每个人都有每个人的方式,几乎是各有千秋。然而借鉴别人成功的代码,绝对是有益无害,因此,多看那些经过千锤百炼凝出来的经典代码,是进阶的最好方法。xml   文件:SimpleDocumentParser.php
  1. <?php/** *========================================================= * * @author hahawen(大龄青年) * @since 2004-12-04 * @copyright Copyright (c) 2004, NxCoder Group * *========================================================= *//** * class SimpleDocumentParser * use SAX parse xml file, and build SimpleDocumentObject * all this pachage's is work for xml file, and method is action as DOM. * * @package SmartWeb.common.xml * @version 1.0 */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($xmlParser,XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($xmlParser,XML_OPTION_SKIP_WHITE, 1); xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); 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)),
  2.         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) { if($this->currentName==$name) { $tag = $this->currentNO->getSeq(); $this->currentNO = $this->currentNO->getPNodeObject(); if($this->currentAttribute!=null && sizeof($this->currentAttribute)>0) $this->currentNO->setValue($name, array('value'=>$this->currentValue,
  3.        'attrs'=>$this->currentAttribute)); else $this->currentNO->setValue($name, $this->currentValue); $this->currentNO->removeNode($tag); } else { $this->currentNO = (is_a($this->currentNO, 'SimpleDocumentRoot'))? null:
  4.         $this->currentNO->getPNodeObject(); } } private function characterData($parser, $data) { $this->currentValue = iconv('UTF-8', 'GB2312', $data); } function __destruct() { unset($this->domRootObject); }}?>
复制代码
  文件:SimpleDocumentBase.php
  1. <?php/** *========================================================= * * @author hahawen(大龄青年) * @since 2004-12-04 * @copyright Copyright (c) 2004, NxCoder Group * *========================================================= *//** * abstract class SimpleDocumentBase * base class for xml file parse * all this pachage's is work for xml file, and method is action as DOM. * * 1\ add/update/remove data of xml file. * 2\ explode data to array. * 3\ rebuild xml file * * @package SmartWeb.common.xml * @abstract * @version 1.0 */abstract class SimpleDocumentBase{ private $nodeTag = null; private $attributes = array(); private $values = array(); private $nodes = array(); function __construct($nodeTag) { $this->nodeTag = $nodeTag; } public function getNodeTag() { return $this->nodeTag; } public function setValues($values){ $this->values = $values; } public function setValue($name, $value) { $this->values[$name] = $value; } public function getValue($name=null) { return $name==null? $this->values: $this->values[$name]; } public function removeValue($name) { unset($this->values["$name"]); } public function setAttributes($attributes){ $this->attributes = $attributes; } public function setAttribute($name, $value) { $this->attributes[$name] = $value; } public function getAttribute($name=null) { return $name==null? $this->attributes: $this->attributes[$name]; } public function removeAttribute($name) { unset($this->attributes["$name"]); } public function getNodesSize() { return sizeof($this->nodes); } protected function setNode($name, $nodeId) { $this->nodes[$name] = $nodeId; } public abstract function createNode($name, $attributes); public abstract function removeNode($name); public abstract function getNode($name=null); protected function getNodeId($name=null) { return $name==null? $this->nodes: $this->nodes[$name]; } protected function createNodeByName($rootNodeObj, $name, $attributes, $pId) { $tmpObject = $rootNodeObj->createNodeObject($pId, $name, $attributes); $key = isset($attributes[id])? $name.'_'.$attributes[id]: $name.'_'.$this->getNodesSize(); $this->setNode($key, $tmpObject->getSeq()); return $tmpObject; } protected function removeNodeByName($rootNodeObj, $name) { $rootNodeObj->removeNodeById($this->getNodeId($name)); if(sizeof($this->nodes)==1) $this->nodes = array(); else unset($this->nodes[$name]); } protected function getNodeByName($rootNodeObj, $name=null) { if($name==null) { $tmpList = array(); $tmpIds = $this->getNodeId(); foreach($tmpIds as $key=>$id) $tmpList[$key] = $rootNodeObj->getNodeById($id); return $tmpList; } else { $id = $this->getNodeId($name); if($id===null) { $tmpIds = $this->getNodeId(); foreach($tmpIds as $tkey=>$tid) { if(strpos($key, $name)==0) { $id = $tid; break; } } } return $rootNodeObj->getNodeById($id); } } public function findNodeByPath($path) { $pos = strpos($path, '|'); if($pos<=0) { return $this->getNode($path); } else { $tmpObj = $this->getNode(substr($path, 0, $pos)); return is_object($tmpObj)? $tmpObj->findNodeByPath(substr($path, $pos+1)): null; } } public function getSaveData() { $data = $this->values; if(sizeof($this->attributes)>0) $data[attrs] = $this->attributes; $nodeList = $this->getNode(); if($nodeList==null) return $data; foreach($nodeList as $key=>$node) { $data[$key] = $node->getSaveData(); } return $data; } public function getSaveXml($level=0) { $prefixSpace = str_pad("", $level, "\t"); $str = "$prefixSpace<$this->nodeTag"; foreach($this->attributes as $key=>$value) $str .= " $key=\"$value\""; $str .= ">\r\n"; foreach($this->values as $key=>$value){ if(is_array($value)) { $str .= "$prefixSpace\t<$key"; foreach($value[attrs] as $attkey=>$attvalue) $str .= " $attkey=\"$attvalue\""; $tmpStr = $value[value]; } else { $str .= "$prefixSpace\t<$key"; $tmpStr = $value; } $tmpStr = trim(trim($tmpStr, "\r\n")); $str .= ($tmpStr===null || $tmpStr==="")? " />\r\n": ">$tmpStr</$key>\r\n"; } foreach($this->getNode() as $node) $str .= $node->getSaveXml($level+1)."\r\n"; $str .= "$prefixSpace</$this->nodeTag>"; return $str; } function __destruct() { unset($this->nodes, $this->attributes, $this->values); }}?>
复制代码

  怎么培养啊 别光说不练啊,好 ,比如新人入门自己步是配置环境,虽然现在都有很多的集成环境,但是真实的体验下配置环境还是会有很多帮助,不论是你以后工作还是在真实的linux下开发。
若天明 该用户已被删除
沙发
 楼主| 发表于 2015-2-4 13:13:44 | 只看该作者
当然这种网站的会员费就几十块钱。
金色的骷髅 该用户已被删除
板凳
发表于 2015-2-7 02:26:27 | 只看该作者
要进行开发,搭建环境是首先需要做的事,windows下面我习惯把环境那个安装在C盘下面,因为我配的环境经常出现诡异事件,什么事都没做环境有的时候就不能用啦。
小魔女 该用户已被删除
地板
发表于 2015-2-19 21:02:06 | 只看该作者
Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81
莫相离 该用户已被删除
5#
发表于 2015-2-27 22:52:53 | 只看该作者
我还是推荐用firefox ,配上firebug 插件调试js能省下不受时间。谷歌的浏览器最好也不少用,因为谷歌的大侠们实在是太天才啦,把一些原来的js代码加了一些特效。
谁可相欹 该用户已被删除
6#
发表于 2015-3-1 18:54:10 | 只看该作者
对于懒惰的朋友,我推荐php的集成环境xampp或者是wamp。这两个软件安装方便,使用简单。但是我还是强烈建议自己动手搭建开发环境。
山那边是海 该用户已被删除
7#
发表于 2015-3-7 14:45:43 | 只看该作者
在我安装pear包的时候老是提示,缺少某某文件,才发现 那群extension 的排列是应该有一点的顺序,而我安装的版本的排序不是正常的排序。没办法我只好把那群冒号加了上去,只留下我需要使用的扩展。
简单生活 该用户已被删除
8#
发表于 2015-3-19 02:36:22 | 只看该作者
使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的
小妖女 该用户已被删除
9#
发表于 2015-3-27 00:09:46 | 只看该作者
如果你已经到这种程度了,那么你已经可以做我的老师了。其实php也分很多的区域,
老尸 该用户已被删除
10#
发表于 2015-3-28 20:21:26 | 只看该作者
说php的话,首先得提一下数组,开始的时候我是最烦数组的,总是被弄的晕头转向,不过后来呢,我觉得数组里php里最强大的存储方法,所以建议新手们要学好数组。
精灵巫婆 该用户已被删除
11#
发表于 2015-3-28 23:59:35 | 只看该作者
有时候汉字的空格也能导致页面出错,所以在写代码的时候,要输入空格最好用引文模式。
只想知道 该用户已被删除
12#
发表于 2015-4-1 03:34:51 | 只看该作者
微软最近出的新字体“微软雅黑”,虽然是挺漂亮的,不过firefox  支持的不是很好,所以能少用还是少用的好。
蒙在股里 该用户已被删除
13#
发表于 2015-4-11 18:08:47 | 只看该作者
Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81
变相怪杰 该用户已被删除
14#
发表于 2015-4-12 17:38:26 | 只看该作者
多看优秀程序员编写的代码,仔细理解他们解决问题的方法,对自身有很大的帮助。
深爱那片海 该用户已被删除
15#
发表于 2015-4-16 00:13:01 | 只看该作者
当然这种网站的会员费就几十块钱。
冷月葬花魂 该用户已被删除
16#
发表于 2015-4-16 15:12:53 | 只看该作者
多看优秀程序员编写的代码,仔细理解他们解决问题的方法,对自身有很大的帮助。
愤怒的大鸟 该用户已被删除
17#
发表于 2015-4-30 03:45:23 | 只看该作者
本人接触php时间不长,算是phper中的小菜鸟一只吧。由于刚开始学的时候没有名师指,碰过不少疙瘩,呗很多小问题卡过很久,白白浪费不少宝贵的时间,在次分享一些子的学习的心得。
小女巫 该用户已被删除
18#
发表于 2015-5-2 03:23:25 | 只看该作者
刚开始安装php的时候,我图了个省事,把php的扩展全都打开啦(就是把php.ini 那一片 extension 前面的冒号全去掉啦),这样自然有好处,以后不用再需要什么功能再来打开。
爱飞 该用户已被删除
19#
发表于 2015-5-8 01:35:38 | 只看该作者
有时候汉字的空格也能导致页面出错,所以在写代码的时候,要输入空格最好用引文模式。
第二个灵魂 该用户已被删除
20#
发表于 2015-6-9 22:04:56 | 只看该作者
其实也不算什么什么心得,在各位大侠算是小巫见大巫了吧,望大家不要见笑,若其中有错误的地方请各位大虾斧正。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-10 15:11

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表