服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - PHP教程 - PHP解析xml格式数据工具类示例

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

2019-10-26 15:56疯狂的代码-Fire PHP教程

这篇文章主要介绍了PHP解析xml格式数据工具类,涉及php针对xml格式数据节点添加、获取、解析等相关操作技巧,需要的朋友可以参考下

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

 

  1. class ome_xml { 
  2.   /** 
  3.   * xml资源 
  4.   * 
  5.   * @var    resource 
  6.   * @see    xml_parser_create() 
  7.   */ 
  8.   public $parser; 
  9.   /** 
  10.   * 资源编码 
  11.   * 
  12.   * @var    string 
  13.   */ 
  14.   public $srcenc; 
  15.   /** 
  16.   * target encoding 
  17.   * 
  18.   * @var    string 
  19.   */ 
  20.   public $dstenc; 
  21.   /** 
  22.   * the original struct 
  23.   * 
  24.   * @access  private 
  25.   * @var    array 
  26.   */ 
  27.   public $_struct = array(); 
  28.   /** 
  29.   * Constructor 
  30.   * 
  31.   * @access    public 
  32.   * @param    mixed    [$srcenc] source encoding 
  33.   * @param    mixed    [$dstenc] target encoding 
  34.   * @return    void 
  35.   * @since 
  36.   */ 
  37.   function SofeeXmlParser($srcenc = null, $dstenc = null) { 
  38.     $this->srcenc = $srcenc; 
  39.     $this->dstenc = $dstenc; 
  40.     // initialize the variable. 
  41.     $this->parser = null
  42.     $this->_struct = array(); 
  43.   } 
  44.   /** 
  45.   * Parses the XML file 
  46.   * 
  47.   * @access    public 
  48.   * @param    string    [$file] the XML file name 
  49.   * @return    void 
  50.   * @since 
  51.   */ 
  52.   function xml2array($file) { 
  53.     //$this->SofeeXmlParser('utf-8'); 
  54.   $data = file_get_contents($file); 
  55.     $this->parseString($data); 
  56.     return $this->getTree(); 
  57.   } 
  58.   function xml3array($file){ 
  59.   $data = file_get_contents($file); 
  60.   $this->parseString($data); 
  61.   return $this->_struct; 
  62.   } 
  63.   /** 
  64.   * Parses a string. 
  65.   * 
  66.   * @access    public 
  67.   * @param    string    data XML data 
  68.   * @return    void 
  69.   */ 
  70.   function parseString($data) { 
  71.     if ($this->srcenc === null) { 
  72.       $this->parser = xml_parser_create(); 
  73.     } else { 
  74.       if($this->parser = xml_parser_create($this->srcenc)) { 
  75.         return 'Unable to create XML parser resource with '. $this->srcenc .' encoding.'
  76.       } 
  77.     } 
  78.     if ($this->dstenc !== null) { 
  79.       @xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $this->dstenc) or die('Invalid target encoding'); 
  80.     } 
  81.     xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);  // lowercase tags 
  82.     xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);    // skip empty tags 
  83.     if (!xml_parse_into_struct($this->parser, $data, $this->_struct)) { 
  84.       /*printf("XML error: %s at line %d", 
  85.           xml_error_string(xml_get_error_code($this->parser)), 
  86.           xml_get_current_line_number($this->parser) 
  87.       );*/ 
  88.       $this->free(); 
  89.       return false
  90.     } 
  91.     $this->_count = count($this->_struct); 
  92.     $this->free(); 
  93.   } 
  94.   /** 
  95.   * return the data struction 
  96.   * 
  97.   * @access    public 
  98.   * @return    array 
  99.   */ 
  100.   function getTree() { 
  101.     $i = 0; 
  102.     $tree = array(); 
  103.     $tree = $this->addNode( 
  104.       $tree, 
  105.       $this->_struct[$i]['tag'], 
  106.       (isset($this->_struct[$i]['value'])) ? $this->_struct[$i]['value'] : ''
  107.       (isset($this->_struct[$i]['attributes'])) ? $this->_struct[$i]['attributes'] : ''
  108.       $this->getChild($i) 
  109.     ); 
  110.     unset($this->_struct); 
  111.     return $tree; 
  112.   } 
  113.   /** 
  114.   * recursion the children node data 
  115.   * 
  116.   * @access    public 
  117.   * @param    integer    [$i] the last struct index 
  118.   * @return    array 
  119.   */ 
  120.   function getChild(&$i) { 
  121.     // contain node data 
  122.     $children = array(); 
  123.     // loop 
  124.     while (++$i < $this->_count) { 
  125.       // node tag name 
  126.       $tagname = $this->_struct[$i]['tag']; 
  127.       $value = isset($this->_struct[$i]['value']) ? $this->_struct[$i]['value'] : ''
  128.       $attributes = isset($this->_struct[$i]['attributes']) ? $this->_struct[$i]['attributes'] : ''
  129.       switch ($this->_struct[$i]['type']) { 
  130.         case 'open'
  131.           // node has more children 
  132.           $child = $this->getChild($i); 
  133.           // append the children data to the current node 
  134.           $children = $this->addNode($children, $tagname, $value, $attributes, $child); 
  135.           break
  136.         case 'complete'
  137.           // at end of current branch 
  138.           $children = $this->addNode($children, $tagname, $value, $attributes); 
  139.           break
  140.         case 'cdata'
  141.           // node has CDATA after one of it's children 
  142.           $children['value'] .= $value; 
  143.           break
  144.         case 'close'
  145.           // end of node, return collected data 
  146.           return $children; 
  147.           break
  148.       } 
  149.     } 
  150.     //return $children; 
  151.   } 
  152.   /** 
  153.   * Appends some values to an array 
  154.   * 
  155.   * @access    public 
  156.   * @param    array    [$target] 
  157.   * @param    string    [$key] 
  158.   * @param    string    [$value] 
  159.   * @param    array    [$attributes] 
  160.   * @param    array    [$inner] the children 
  161.   * @return    void 
  162.   * @since 
  163.   */ 
  164.   function addNode($target, $key, $value = '', $attributes = '', $child = '') { 
  165.     if (!isset($target[$key]['value']) && !isset($target[$key][0])) { 
  166.       if ($child != '') { 
  167.         $target[$key] = $child; 
  168.       } 
  169.       if ($attributes != '') { 
  170.         foreach ($attributes as $k => $v) { 
  171.           $target[$key][$k] = $v; 
  172.         } 
  173.       } 
  174.       $target[$key]['value'] = $value; 
  175.     } else { 
  176.       if (!isset($target[$key][0])) { 
  177.         // is string or other 
  178.         $oldvalue = $target[$key]; 
  179.         $target[$key] = array(); 
  180.         $target[$key][0] = $oldvalue; 
  181.         $index = 1; 
  182.       } else { 
  183.         // is array 
  184.         $index = count($target[$key]); 
  185.       } 
  186.       if ($child != '') { 
  187.         $target[$key][$index] = $child; 
  188.       } 
  189.       if ($attributes != '') { 
  190.         foreach ($attributes as $k => $v) { 
  191.           $target[$key][$index][$k] = $v; 
  192.         } 
  193.       } 
  194.       $target[$key][$index]['value'] = $value; 
  195.     } 
  196.     return $target; 
  197.   } 
  198.   /** 
  199.   * Free the resources 
  200.   * 
  201.   * @access    public 
  202.   * @return    void 
  203.   **/ 
  204.   function free() { 
  205.     if (isset($this->parser) && is_resource($this->parser)) { 
  206.       xml_parser_free($this->parser); 
  207.       unset($this->parser); 
  208.     } 
  209.   } 

 

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

延伸 · 阅读

精彩推荐
  • PHP教程Yii2语言国际化的配置教程

    Yii2语言国际化的配置教程

    这篇文章主要给大家介绍了关于Yii2语言国际化的配置教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋...

    durban2692019-09-11
  • PHP教程PHP基于面向对象封装的分页类示例

    PHP基于面向对象封装的分页类示例

    这篇文章主要介绍了PHP基于面向对象封装的分页类,结合实例形式分析了php分页类针对页码判断、显示等操作的封装及分页类使用相关操作技巧,需要的朋友...

    小菜鸟有大梦想1462019-05-31
  • PHP教程PHP中实现中文字串截取无乱码的解决方法

    PHP中实现中文字串截取无乱码的解决方法

    这篇文章主要介绍了PHP中实现中文字串截取无乱码的解决方法,直接使用PHP函数substr截取中文字符可能会出现乱码,下面跟随脚本之家小编一起看看具体解决...

    编程成长回忆1812019-10-04
  • PHP教程PHP命名空间与自动加载类详解

    PHP命名空间与自动加载类详解

    这篇文章主要介绍了PHP命名空间与自动加载类,结合实例形式详细分析了php自动加载类与命名空间原理、使用方法及相关操作注意事项,需要的朋友可以参考...

    NameWFY1192019-09-08
  • PHP教程PHP赋值的内部是如何跑的详解

    PHP赋值的内部是如何跑的详解

    这篇文章主要给大家介绍了关于PHP赋值的内部是如何跑的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,...

    写PHP的老王2662019-07-03
  • PHP教程可兼容php5与php7的cURL文件上传功能实例分析

    可兼容php5与php7的cURL文件上传功能实例分析

    这篇文章主要介绍了可兼容php5与php7的cURL文件上传功能,结合实例形式分析了针对php5与php7版本在使用curl进行文件上传时的相关判定与具体操作技巧,需要的...

    straiway3042019-10-08
  • PHP教程PHP bin2hex()函数基础实例讲解

    PHP bin2hex()函数基础实例讲解

    今天小编就为大家分享一篇关于PHP bin2hex()函数基础实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来...

    php参考手册5252019-06-18
  • PHP教程PHPExcel 修改已存在Excel的方法

    PHPExcel 修改已存在Excel的方法

    下面小编就为大家分享一篇PHPExcel 修改已存在Excel的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    漫步先生1982019-10-09