登录  /  注册
首页 > web前端 > js教程 > 正文

PHP解析xml格式数据工具类实例分享

小云云
发布: 2018-01-09 09:41:22
原创
1323人浏览过

本文主要介绍了php解析xml格式数据工具类,涉及php针对xml格式数据节点添加、获取、解析等相关操作技巧,需要的朋友可以参考下,希望能帮助到大家。

本文实例讲述了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][&#39;tag&#39;];
      $value = isset($this->_struct[$i][&#39;value&#39;]) ? $this->_struct[$i][&#39;value&#39;] : &#39;&#39;;
      $attributes = isset($this->_struct[$i][&#39;attributes&#39;]) ? $this->_struct[$i][&#39;attributes&#39;] : &#39;&#39;;
      switch ($this->_struct[$i][&#39;type&#39;]) {
        case &#39;open&#39;:
          // 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 &#39;complete&#39;:
          // at end of current branch
          $children = $this->addNode($children, $tagname, $value, $attributes);
          break;
        case &#39;cdata&#39;:
          // node has CDATA after one of it&#39;s children
          $children[&#39;value&#39;] .= $value;
          break;
        case &#39;close&#39;:
          // 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 = &#39;&#39;, $attributes = &#39;&#39;, $child = &#39;&#39;) {
    if (!isset($target[$key][&#39;value&#39;]) && !isset($target[$key][0])) {
      if ($child != &#39;&#39;) {
        $target[$key] = $child;
      }
      if ($attributes != &#39;&#39;) {
        foreach ($attributes as $k => $v) {
          $target[$key][$k] = $v;
        }
      }
      $target[$key][&#39;value&#39;] = $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 != &#39;&#39;) {
        $target[$key][$index] = $child;
      }
      if ($attributes != &#39;&#39;) {
        foreach ($attributes as $k => $v) {
          $target[$key][$index][$k] = $v;
        }
      }
      $target[$key][$index][&#39;value&#39;] = $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);
    }
  }
登录后复制

相关推荐:

详解PHP对xml文件增删改查

php实现输出xml属性

java将XML文档转换成json格式数据

以上就是PHP解析xml格式数据工具类实例分享的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号