当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP解析xml格式数据工具类示例

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

2018年01月26日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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互相转换工具:

在线格式化xml/在线压缩xml

xml在线压缩/格式化工具:

xml代码在线格式化美化工具:

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网