搜索
首页php教程php手册ThinkPHP XML格式转换成数组

XML格式转换成数组 用递归操作很简单,大家可以下载附件测试
// Xml 转 数组, 包括根键,忽略空元素和属性,尚有重大错误<br> public function xml_to_array( $xml )<br>     {<br>         $reg = "/]*?>([\\x00-\\xFF]*?)/";<br>         if(preg_match_all($reg, $xml, $matches))<br>         {<br>             $count = count($matches[0]);<br>             $arr = array();<br>             for($i = 0; $i              {<br>                 $key = $matches[1][$i];<br>                 $val = $this->xml_to_array( $matches[2][$i] );  // 递归<br>                 if(array_key_exists($key, $arr))<br>                 {<br>                     if(is_array($arr[$key]))<br>                     {<br>                         if(!array_key_exists(0,$arr[$key])) <br>                         {<br>                             $arr[$key] = array($arr[$key]);<br>                         }<br>                     }else{<br>                         $arr[$key] = array($arr[$key]);<br>                     }<br>                     $arr[$key][] = $val;<br>                 }else{<br>                     $arr[$key] = $val;<br>                 }<br>             }<br>             return $arr;<br>         }else{<br>             return $xml;<br>         }<br>     }<br> <br> <br> // Xml 转 数组, 不包括根键<br> public function xmltoarray( $xml )<br>     {<br> <br>         $arr = $this->xml_to_array($xml);<br>         $key = array_keys($arr);<br>         return $arr[$key[0]];<br>     }第二种方法,预防递归溢出class XML<br> {<br> <br> public $parser = NULL;<br> public $document = NULL;<br> public $parent = NULL;<br> public $stack = NULL;<br> public $last_opened_tag = NULL;<br> <br> public function XML( )<br>     {<br>         $this->parser = xml_parser_create( );<br>         xml_parser_set_option( $this->parser, XML_OPTION_CASE_FOLDING, false );<br>         xml_set_object( $this->parser, $this );<br>         xml_set_element_handler( $this->parser, "open", "close" );<br>         xml_set_character_data_handler( $this->parser, "data" );<br>     }<br> <br> public function destruct( )<br>     {<br>         xml_parser_free( $this->parser );<br>     }<br> <br> public function &parse( &$data )<br>     {<br>         $this->document = array( );<br>         $this->stack = array( );<br>         $this->parent =& $this->document;<br>         $parsedata = xml_parse( $this->parser, $data, true ) ? $this->document : NULL;<br>         return $parsedata;<br>     }<br> <br> public function open( &$parser, $tag, $attributes )<br>     {<br>         $this->data = "";<br>         $this->last_opened_tag = $tag;<br>         if ( is_array( $this->parent ) && array_key_exists( $tag, $this->parent ) )<br>         {<br>             if ( is_array( $this->parent[$tag] ) && array_key_exists( 0, $this->parent[$tag] ) )<br>             {<br>                 $key = count_numeric_items( $this->parent[$tag] );<br>             }<br>             else<br>             {<br>                 if ( array_key_exists( "{$tag} attr", $this->parent ) )<br>                 {<br>                     $arr = array(<br>                         "0 attr" => $this->parent["{$tag} attr"],<br>                         $this->parent[$tag]<br>                     );<br>                     unset( $this->parent["{$tag} attr"] );<br>                 }<br>                 else<br>                 {<br>                     $arr = array(<br>                         $this->parent[$tag]<br>                     );<br>                 }<br>                 $this->parent[$tag] =& $arr;<br>                 $key = 1;<br>             }<br>             $this->parent =& $this->parent[$tag];<br>         }<br>         else<br>         {<br>             $key = $tag;<br>         }<br>         if ( $attributes )<br>         {<br>             $this->parent["{$key} attr"] = $attributes;<br>         }<br>         $this->parent =& $this->parent[$key];<br>         $this->stack[] =& $this->parent;<br>     }<br> <br> public function data( &$parser, $data )<br>     {<br>         if ( $this->last_opened_tag != NULL )<br>         {<br>             $this->data .= $data;<br>         }<br>     }<br> <br> public function close( &$parser, $tag )<br>     {<br>         if ( $this->last_opened_tag == $tag )<br>         {<br>             $this->parent = $this->data;<br>             $this->last_opened_tag = NULL;<br>         }<br>         array_pop( $this->stack );<br>         if ( $this->stack )<br>         {<br>             $this->parent =& $this->stack[count( $this->stack ) - 1];<br>         }<br>     }<br> <br> }<br> <br> function &XML_unserialize( &$xml )<br> {<br>     $xml_parser = new XML( );<br>     $data =& $xml_parser->parse( $xml );<br>     $xml_parser->destruct( );<br>     return $data;<br> }<br> <br> function &XML_serialize( &$data, $level = 0, $prior_key = NULL )<br> {<br>     if ( $level == 0 )<br>     {<br>         ob_start( );<br>         echo "<?xml version=\"1.0\" ?>";<br>         echo "\n";<br>     }<br>     while ( list( $key, $value ) = each( $data ) )<br>     {<br>         if ( !strpos( $key, " attr" ) )<br>         {<br>             if ( is_array( $value ) && array_key_exists( 0, $value ) )<br>             {<br>                 xml_serialize( $value, $level, $key );<br>             }<br>             else<br>             {<br>                 $tag = $prior_key ? $prior_key : $key;<br>                 echo str_repeat( "\t", $level );<br>                 echo "                 echo $tag;<br>                 if ( array_key_exists( "{$key} attr", $data ) )<br>                 {<br>                     while ( list( $attr_name, $attr_value ) = each( $data["{$key} attr"] ) )<br>                     {<br>                         echo " ";<br>                         echo $attr_name;<br>                         echo "=\"";<br>                         echo htmlspecialchars( $attr_value );<br>                         echo "\"";<br>                     }<br>                     reset( $data["{$key} attr"] );<br>                 }<br>                 if ( is_null( $value ) )<br>                 {<br>                     echo " />\n";<br>                 }<br>                 else if ( !is_array( $value ) )<br>                 {<br>                     echo ">";<br>                     echo htmlspecialchars( $value );<br>                     echo "{$tag}>\n";<br>                 }<br>                 else<br>                 {<br>                     echo ">\n";<br>                     echo xml_serialize( $value, $level + 1 );<br>                     echo str_repeat( "\t", $level );<br>                     echo "{$tag}>\n";<br>                 }<br>             }<br>         }<br>     }<br>     reset( $data );<br>     if ( $level == 0 )<br>     {<br>         $str =& ob_get_contents( );<br>         ob_end_clean( );<br>         return $str;<br>     }<br> }<br> <br> function count_numeric_items( &$array )<br> {<br>     return is_array( $array ) ? count( array_filter( array_keys( $array ), "is_numeric" ) ) : 0;<br> }

附件 IndexModel.class.zip ( 705 B 下载:101 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用