Home > php教程 > php手册 > ThinkPHP XML格式转换成数组

ThinkPHP XML格式转换成数组

WBOY
Release: 2016-06-07 11:43:22
Original
1664 people have browsed it

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元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template