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

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

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,$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程序设计有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


服务器优化必备:深入了解PHP8底层开发原理
Golang的网络编程:如何快速构建高性能的网络应用?
Golang和其他编程语言的对比:为什么它的开发效率更高?
PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用
将字符重新排列以形成回文(如果可能)在C++中
掌握PHP8底层开发原理和新特性:创建高效可扩展的应用程序
服务器性能优化必学:掌握PHP8底层开发原理
PHP8新特性和底层开发原理详解:优化应用性能的终极指南
将 C/C++ 代码转换为汇编语言
深入研究PHP8底层开发原理:创建高效可扩展的应用程序
C++程序查找法向量和迹
PHP8底层开发原理实战指南:提升服务器效能
重排数组,使得当 i 为偶数时,arr[i] >= arr[j],当 i 为奇数时,arr[i] <= arr[j],其中 j < i,使用 C++ 语言实现
Golang的垃圾回收:为什么它可以减少开发人员的负担?
C++程序:将一个数组的所有元素复制到另一个数组中
Golang:构建智能系统的基石
为什么AI开发者应该关注Golang?
在C和C++中,逗号(comma)的用法是用来分隔表达式或语句
PHP8底层开发原理解析及新特性应用实例
利用PHP8底层开发原理解析新特性:如何构建出色的Web应用