侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP解析xml格式数据工具类示例

php  /  管理员 发布于 7年前   136

本文实例讲述了PHP解析xml格式数据工具类。分享给大家供大家参考,具体如下:

class ome_xml {  /**  * xml资源  *  * @var    resource  * @see    xml_parser_create()  */  public $parser;  /**  * 资源编码  *  * @var    string  */  public $srcenc;  /**  * target encoding  *  * @var    string  */  public $dstenc;  /**  * the original struct  *  * @access  private  * @var    array  */  public $_struct = array();  /**  * Constructor  *  * @access    public  * @param    mixed    [$srcenc] source encoding  * @param    mixed    [$dstenc] target encoding  * @return    void  * @since  */  function SofeeXmlParser($srcenc = null, $dstenc = null) {    $this->srcenc = $srcenc;    $this->dstenc = $dstenc;    // initialize the variable.    $this->parser = null;    $this->_struct = array();  }  /**  * Parses the XML file  *  * @access    public  * @param    string    [$file] the XML file name  * @return    void  * @since  */  function xml2array($file) {    //$this->SofeeXmlParser('utf-8');  $data = file_get_contents($file);    $this->parseString($data);    return $this->getTree();  }  function xml3array($file){  $data = file_get_contents($file);  $this->parseString($data);  return $this->_struct;  }  /**  * Parses a string.  *  * @access    public  * @param    string    data XML data  * @return    void  */  function parseString($data) {    if ($this->srcenc === null) {      $this->parser = xml_parser_create();    } else {      if($this->parser = xml_parser_create($this->srcenc)) {        return 'Unable to create XML parser resource with '. $this->srcenc .' encoding.';      }    }    if ($this->dstenc !== null) {      @xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $this->dstenc) or die('Invalid target encoding');    }    xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);  // lowercase tags    xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);    // skip empty tags    if (!xml_parse_into_struct($this->parser, $data, $this->_struct)) {      /*printf("XML error: %s at line %d",          xml_error_string(xml_get_error_code($this->parser)),          xml_get_current_line_number($this->parser)      );*/      $this->free();      return false;    }    $this->_count = count($this->_struct);    $this->free();  }  /**  * return the data struction  *  * @access    public  * @return    array  */  function getTree() {    $i = 0;    $tree = array();    $tree = $this->addNode(      $tree,      $this->_struct[$i]['tag'],      (isset($this->_struct[$i]['value'])) ? $this->_struct[$i]['value'] : '',      (isset($this->_struct[$i]['attributes'])) ? $this->_struct[$i]['attributes'] : '',      $this->getChild($i)    );    unset($this->_struct);    return $tree;  }  /**  * recursion the children node data  *  * @access    public  * @param    integer    [$i] the last struct index  * @return    array  */  function getChild(&$i) {    // contain node data    $children = array();    // loop    while (++$i < $this->_count) {      // node tag name      $tagname = $this->_struct[$i]['tag'];      $value = isset($this->_struct[$i]['value']) ? $this->_struct[$i]['value'] : '';      $attributes = isset($this->_struct[$i]['attributes']) ? $this->_struct[$i]['attributes'] : '';      switch ($this->_struct[$i]['type']) {        case 'open':          // node has more children          $child = $this->getChild($i);          // append the children data to the current node          $children = $this->addNode($children, $tagname, $value, $attributes, $child);          break;        case 'complete':          // at end of current branch          $children = $this->addNode($children, $tagname, $value, $attributes);          break;        case 'cdata':          // node has CDATA after one of it's children          $children['value'] .= $value;          break;        case 'close':          // end of node, return collected data          return $children;          break;      }    }    //return $children;  }  /**  * Appends some values to an array  *  * @access    public  * @param    array    [$target]  * @param    string    [$key]  * @param    string    [$value]  * @param    array    [$attributes]  * @param    array    [$inner] the children  * @return    void  * @since  */  function addNode($target, $key, $value = '', $attributes = '', $child = '') {    if (!isset($target[$key]['value']) && !isset($target[$key][0])) {      if ($child != '') {        $target[$key] = $child;      }      if ($attributes != '') {        foreach ($attributes as $k => $v) {          $target[$key][$k] = $v;        }      }      $target[$key]['value'] = $value;    } else {      if (!isset($target[$key][0])) {        // is string or other        $oldvalue = $target[$key];        $target[$key] = array();        $target[$key][0] = $oldvalue;        $index = 1;      } else {        // is array        $index = count($target[$key]);      }      if ($child != '') {        $target[$key][$index] = $child;      }      if ($attributes != '') {        foreach ($attributes as $k => $v) {          $target[$key][$index][$k] = $v;        }      }      $target[$key][$index]['value'] = $value;    }    return $target;  }  /**  * Free the resources  *  * @access    public  * @return    void  **/  function free() {    if (isset($this->parser) && is_resource($this->parser)) {      xml_parser_free($this->parser);      unset($this->parser);    }  }}

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线XML/JSON互相转换工具:
http://tools..net.cn/code/xmljson

在线格式化XML/在线压缩XML:
http://tools..net.cn/code/xmlformat

XML在线压缩/格式化工具:
http://tools..net.cn/code/xml_format_compress

XML代码在线格式化美化工具:
http://tools..net.cn/code/xmlcodeformat

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP针对XML文件操作技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • php获取通过http协议post提交过来xml数据及解析xml
  • php解析xml方法实例详解
  • php解析xml 的四种简单方法(附实例)
  • PHP用SAX解析XML的实现代码与问题分析
  • php遍历解析xml字符串的方法
  • php 解析xml 的四种方法详细介绍
  • PHP基于SimpleXML生成和解析xml的方法示例
  • PHP使用xpath解析XML的方法详解
  • PHP处理数组和XML之间的互相转换
  • php实现将数组转换为XML的方法
  • PHP简单实现解析xml为数组的方法


  • 上一条:
    PHP中类静态调用和范围解析操作符的区别解析
    下一条:
    PHP简单选择排序(Simple Selection Sort)算法学习
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客