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

xml

文件:SimpleDocumentRoot.php

<?php/** *========================================================= * * @author     hahawen(大龄青年)   * @since      2004-12-04 * @copyright  Copyright (c) 2004, NxCoder Group * *========================================================= *//** * class SimpleDocumentRoot * xml root class, include values/attributes/subnodes. * all this pachage's is work for xml file, and method is action as DOM. * * @package SmartWeb.common.xml * @version 1.0 */class SimpleDocumentRoot extends SimpleDocumentBase{ private $prefixStr = '<?xml version="1.0" encoding="utf-8" ?>'; private $nodeLists = array(); function __construct($nodeTag) {        parent::__construct($nodeTag); }    public function createNodeObject($pNodeId, $name, $attributes)    {     $seq = sizeof($this->nodeLists);     $tmpObject = new SimpleDocumentNode($this, $pNodeId, $name, $seq);     $tmpObject->setAttributes($attributes);     $this->nodeLists[$seq] = $tmpObject;     return $tmpObject;    }    public function removeNodeById($id)    {        if(sizeof($this->nodeLists)==1)            $this->nodeLists = array();     else      unset($this->nodeLists[$id]);    }    public function getNodeById($id)    {     return $this->nodeLists[$id];    }    public function createNode($name, $attributes)    {        return $this->createNodeByName($this, $name, $attributes, -1);    }    public function removeNode($name)    {        return $this->removeNodeByName($this, $name);    }    public function getNode($name=null)    {        return $this->getNodeByName($this, $name);    }    public function getSaveXml()    {     $prefixSpace = "";     $str = $this->prefixStr."\r\n";     return $str.parent::getSaveXml(0);    }}?>      

  
  文件:SimpleDocumentNode.php

<?php/** *========================================================= * * @author     hahawen(大龄青年)   * @since      2004-12-04 * @copyright  Copyright (c) 2004, NxCoder Group * *========================================================= *//** * class SimpleDocumentNode * xml Node class, include values/attributes/subnodes. * all this pachage's is work for xml file, and method is action as DOM. * * @package SmartWeb.common.xml * @version 1.0 */class SimpleDocumentNode extends SimpleDocumentBase{ private $seq = null; private $rootObject = null;    private $pNodeId = null;    function __construct($rootObject, $pNodeId, $nodeTag, $seq)    {     parent::__construct($nodeTag);        $this->rootObject = $rootObject;        $this->pNodeId = $pNodeId;        $this->seq = $seq;    }    public function getPNodeObject()    {     return ($this->pNodeId==-1)? $this->rootObject: $this->rootObject->getNodeById($this->pNodeId);    }    public function getSeq(){     return $this->seq;    }    public function createNode($name, $attributes)    {        return $this->createNodeByName($this->rootObject, $name, $attributes, $this->getSeq());    }    public function removeNode($name)    {        return $this->removeNodeByName($this->rootObject, $name);    }    public function getNode($name=null)    {        return $this->getNodeByName($this->rootObject, $name);    }}?>      

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索return
, function
, this
, public
, name
, 轻松解析
Attributes
xml文档解析、ie11解析xml文档代码、java解析一个xml文档、aop配置xml文档的解析、dom4j解析xml文档,以便于您获取更多的相关知识。

时间: 2024-08-30 02:23:43

使用PHP 5.0 轻松解析XML文档(3)的相关文章

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

xml 下面是通过createNode()函数,添加商品, 然后显示添加后的结果 <?xml version="1.0" encoding="GB2312" ?><shop> <name>华联</name> <address>北京长安街-9999号</address> <desc>连锁超市</desc> <telphone>123456789</telp

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

xml 下面是通过getNode()函数,返回某一个分类下的所有商品的信息商品名: food11Array( [name] => food11 [price] => 12.90)Array( [id] => food11)商品名:food12Array( [name] => food12 [price] => 22.10 [desc] => Array ( [value] => 好东西推荐 [attrs] => Array ( [creator] =>

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

xml 下面是例子运行对结果: 下面是通过函数getSaveData()返回的整个xml数据的数组 Array( [name] => 华联 [address] => 北京长安街-9999号 [desc] => 连锁超市 [cat_food] => Array ( [attrs] => Array ( [id] => food ) [goods_food11] => Array ( [name] => food11 [price] => 12.90 [a

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

xml 用sax方式的时候,要自己构建3个函数,而且要直接用这三的函数来返回数据, 要求较强的逻辑. 在处理不同结构的xml的时候, 还要重新进行构造这三个函数,麻烦! 用dom方式,倒是好些,但是他把每个节点都看作是一个node,操作起来要写好多的代码, 麻烦! 网上有好多的开源的xml解析的类库, 以前看过几个,但是心里总是觉得不踏实,感觉总是跟在别人的屁股后面. 这几天在搞java, 挺累的,所以决定换换脑袋,写点php代码,为了防止以后xml解析过程再令我犯难,就花了一天的时间写了下面一

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

xml 文件:SimpleDocumentParser.php <?php/** *========================================================= * * @author hahawen(大龄青年) * @since 2004-12-04 * @copyright Copyright (c) 2004, NxCoder Group * *======================================================

使用PHP 5.0 轻松解析XML文档

用sax方式的时候,要自己构建3个函数,而且要直接用这三的函数来返回数据, 要求较强的逻辑. 在处理不同结构的XML的时候, 还要重新进行构造这三个函数,麻烦! 用dom方式,倒是好些,但是他把每个节点都看作是一个node,操作起来要写好多的代码, 麻烦! 网上有好多的开源的xml解析的类库, 以前看过几个,但是心里总是觉得不踏实,感觉总是跟在别人的屁股后面. 这几天在搞Java, 挺累的,所以决定换换脑袋,写点PHP代码,为了防止以后xml解析过程再令我犯难,就花了一天的时间写了下面一个xml

解析xml文档的一个简单php类

xml //原创,可以自由使用,欢迎提出改进意见, <?PHP//xml中的元素class XMLTag{ var $parent;//父节点 var $child;//子节点 var $attribute;//本节点属性 var $data;//本节点数据 var $TagName;//本节点名称 var $depth;//本节点的深度,根节点为1 function XMLTag($tag='') {  $this->attribute = array();  $this->child

VC++中利用MSXML解析XML文档

c++|xml 四.程序实现 下面通过一个具体的实例来说明在VC++中如何利用MSXML解析XML文档. (1)源XML文档(xmlfile.xml)如下: <?xml version="1.0" encoding="GB2312"?> <Device id="10041" name="设备1"> <Type>13</Type> <TypeName>保护</Ty

使用Digester解析XML文档示例

xml|示例 一. 概述 解析XML文档方面有不少的方法,也有不少的工具包可用,这里介绍的是来自Apache Jakarta的Digester,使用其解析XML文件非常方便而不需要过多的关心底层的具体解析过程. Digester最早出现在Struts中,后来随着Struts的发展以及其的公用性而被提到commens中独自立项,其底层实现的是SAX解析.当前版本为:1.5 二. 正文 1. 安装与配置 使用Digester需要如下几个软件包: Digester ,BeanUtils, Collec