加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

用 PHP5 轻松解析 XML

发布时间:2020-03-20 07:45:23 所属栏目:PHP教程 来源:互联网
导读:用 PHP5 轻松解析 XML

<?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 = '';
    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;
     }

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读