• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

使用PHP5.0轻松解析XML文档(2)

发布: 2007-6-21 15:32 | 作者:   | 来源:   | 查看: 17次 | 进入软件测试论坛讨论

领测软件测试网

   
  文件: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);
    }
  }
  ?>
  
  下面是例子运行对结果:
  
    下面是通过函数getSaveData()返回的整个xml数据的数组
  
  Array
  (
    [name] => 华联
    [address] => 北京长安街-9999号
    [desc] => 连锁超市
    [cat_food] => Array
      (
        [attrs] => Array
          (
            [id] => food
          )
        [goods_food11] => Array
          (
            [name] => food11
            [price] => 12.90
            [attrs] => Array
              (
                [id] => food11
              )
          )
        [goods_food12] => Array
          (
            [name] => food12
            [price] => 22.10
            [desc] => Array
              (
                [value] => 好东西推荐
                [attrs] => Array
                  (
                    [creator] => hahawen
                  )
              )
            [attrs] => Array
              (
                [id] => food12
              )
          )
      )
    [cat_1] => Array
      (
        [goods_tel21] => Array
          (
            [name] => tel21
            [price] => 1290
            [attrs] => Array
              (
                [id] => tel21
              )
          )
      )
    [cat_coat] => Array
      (
        [attrs] => Array
          (
            [id] => coat
          )
        [goods_coat31] => Array
          (
            [name] => coat31
            [price] => 112
            [attrs] => Array
              (
                [id] => coat31
              )
          )
        [goods_coat32] => Array
          (
            [name] => coat32
            [price] => 45
            [attrs] => Array
              (
                [id] => coat32
              )
          )
      )
    [special_hot] => Array
      (
        [attrs] => Array
          (
            [id] => hot
          )
        [goods_0] => Array
          (
            [name] => hot41
            [price] => 99
          )
      )
  )
  
  下面是通过setValue()函数,给给根节点添加信息,添加后显示出结果xml文件的内容
  
  <?xml version="1.0" encoding="GB2312" ?>
  <shop>
   <name>华联</name>
   <address>北京长安街-9999号</address>
   <desc>连锁超市</desc>
   <telphone>123456789</telphone>
   <cat id="food">
   <goods id="food11">
    <name>food11</name>
    <price>12.90</price>
   </goods>
   <goods id="food12">
    <name>food12</name>
    <price>22.10</price>
    <desc creator="hahawen">好东西推荐</desc>
   </goods>
   </cat>
   <cat>
   <goods id="tel21">
    <name>tel21</name>
    <price>1290</price>
   </goods>
   </cat>
   <cat id="coat">
   <goods id="coat31">
    <name>coat31</name>
    <price>112</price>
   </goods>
   <goods id="coat32">
    <name>coat32</name>
    <price>45</price>
   </goods>
   </cat>
   <special id="hot">
   <goods>
    <name>hot41</name>
    <price>99</price>
   </goods>
   </special>
  </shop>
  
  下面是通过getNode()函数,返回某一个分类下的所有商品的信息商品名:
  
  food11Array
  (
    [name] => food11
    [price] => 12.90
  )
  Array
  (
    [id] => food11
  )
  商品名:food12Array
  (
    [name] => food12
    [price] => 22.10
    [desc] => Array
      (
        [value] => 好东西推荐
        [attrs] => Array
          (
            [creator] => hahawen
          )
      )
  )
  Array
  (
    [id] => food12
  )
  
  
    下面是通过findNodeByPath()函数,返回某一商品的信息商品名:
  food11Array
  (
    [name] => food11
    [price] => 12.90
  )
  Array
  (
    [id] => food11
  )
  
  下面是通过setValue()函数,给商品"food11"添加属性, 然后显示添加后的结果
  
  <?xml version="1.0" encoding="GB2312" ?>
  <shop>
   <name>华联</name>
   <address>北京长安街-9999号</address>
   <desc>连锁超市</desc>
   <telphone>123456789</telphone>
   <cat id="food">
   <goods id="food11">
    <name>food11</name>
    <price>12.90</price>
    <leaveword author="hahawen" date="2004-12-05">这个商品不错</leaveword>
   </goods>
   <goods id="food12">
    <name>food12</name>
    <price>22.10</price>
    <desc creator="hahawen">好东西推荐</desc>
   </goods>
   </cat>
   <cat>
   <goods id="tel21">
    <name>tel21</name>
    <price>1290</price>

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网