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

2016-01-29 13:01 2 1 收藏

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

【 tulaoshi.com - PHP 】

文件: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."rn";     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);    }}?>      


(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/php/)

来源:https://www.tulaoshi.com/n/20160129/1488584.html

延伸阅读
标签: Web开发
在本文中主要就如何用SAX解析xml文档进行说明。 要解析的xml片段如下: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Books> <Book id="8542f26f-80d4-4b7d-ab25-f80f72a852ef"> <name id="201"> <strings> <entry> <key>en_US</key> <value> thinking in Java</value> ...
标签: Web开发
/// summary /// author飞鹰@ASPCool.com /author /// description本文介绍在.NET中查找XML节点的方法。 /desciption /// copyrightASP酷技术资讯网(www.ASPCool.com) /copyright /// /summary 大家在.NET中处理XML文档的时候,经常会需要找到文档中的某个节点的数据。要找到某个节点,有许多种方法,在这里我就把几种常用...
标签: Web开发
       对结点的属性赋值   一旦创建了结点,还要对其属性赋值,如独立的标识符,或者特性值。你要用到SetAttribute方法。该方法接收两个参数— 属性名和属性值。例如,下列代码创建了属性名SHIPPING_DATASOURCE 和属性值NORTH_ATLANTIC_SHIPPING:      objXMLroot.SetAttribute...
标签: Web开发
XML在不同领域有着广泛的应用,比如在科技领域的MathML,无线通信应用的WML,在网络图象方面的SVG等等,我们这里侧重讨论XML在web上的应用。XML在web上应用主要是利用其强大的数据操作能力。一般用XML配合javascript和asp等服务器端程序,可以实现网络上几乎所有的应用需求。 考虑讲解方便,我们在下面介绍一个简单的实例,不包含服...
标签: Web开发
第五章:XML实例解析 提纲: 一:实例效果 二:实例解析 1.定义新标识。 2.建立XML文档。 3.建立相应的HTML文件。 XML在不同领域有着广泛的应用,比如在科技领域的MathML,无线通信应用的WML,在网络图象方面的SVG等等,我们这里侧重讨论XML在web上的应用。XML在web上应用主要是利用...

经验教程

184

收藏

85
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部