This article mainly introduces the PHP method of XML Format data, interested in the friend's reference, I hope to be helpful to everyone.
Specific as follows:
<?php/** * Xml2array () would convert the given XML text to a array in the XML structure. * link:http://www.bin-co.com/php/scripts/xml2array/* Arguments: $contents-the XML text * $get _attributes-1 or 0. If This is 1 the function would get the attributes as well as the tag values-this results in a different array structure In the return value. * $priority-can be ' tag ' or ' attribute '. This would change the resulting array sturcture. For ' tag ', the tags is given more importance. * Return:the parsed XML in an array form. Use Print_r () to see the resulting array structure. * Examples: $array = Xml2array (file_get_contents (' feed.xml ')); * $array = Xml2array (file_get_contents (' Feed.xml ', 1, ' attribute ')); */function Xml2array ($contents, $get _attributes = 1, $priority = ' tag ') {if (! $contents) return array (); if (!function_exists (' xml_parser_create ')) {//print "' xml_parser_create () ' function not found!"; return Array (); }//Get The XML parser of Php-pHP must has this module for the parser to work $parser = Xml_parser_create ("); Xml_parser_set_option ($parser, xml_option_target_encoding, "UTF-8"); http://minutillo.com/steve/weblog/2004/6/17/ Php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss xml_parser_set_option ($parser, XML_OPTION_ case_folding, 0); Xml_parser_set_option ($parser, Xml_option_skip_white, 1); Xml_parse_into_struct ($parser, Trim ($contents), $xml _values); Xml_parser_free ($parser); if (! $xml _values) return; Hmm ... initializations $xml _array = Array (); $parents = Array (); $opened _tags = Array (); $arr = Array (); $current = & $xml _array; Refference//Go through the tags. $repeated _tag_index = Array (); Multiple tags with same name would be turned to an array foreach ($xml _values as $data) {unset ($attributes, $value ); Remove existing values, or there'll be trouble//This command would extract these variables into the foreach scope Tag (string), type (string), level (int), attributes (array). Extract ($data); We could use the array by itself, but this cooler. $result = Array (); $attributes _data = Array (); if (Isset ($value)) {if ($priority = = ' Tag ') $result = $value; else $result [' value '] = $value; Put the value in a assoc array if we is in the ' Attribute ' mode}//Set the attributes too. if (Isset ($attributes) and $get _attributes) {foreach ($attributes as $attr = + $val) {if ($priority = = ' Tag ') $attributes _data[$attr] = $val; else $result [' attr '] [$attr] = $val; Set all the attributes in an array called ' attr '}}//See tag status and do the needed. if ($type = = "Open") {//The starting of the tag ' <tag> ' $parent [$level-1] = & $current; if (!is_array ($current) or (!in_array ($tag, Array_keys ($current))) {//Insert New tag $current [$tag] = $result; if ($attributes _data) $current [$tag. ' _attr '] = $attributes _data; $repeated_tag_index[$tag. '_' . $level] = 1; $current = & $current [$tag]; } else {//There was another element with the same tag name if (Isset ($current [$tag][0])) {//If there is a 0th E Lement It is already an array $current [$tag] [$repeated _tag_index[$tag. '_' . $level]] = $result; $repeated _tag_index[$tag. '_' . $level]++; } else {//This section would make the value a array if multiple tags with the same name appear together $curren t[$tag] = Array ($current [$tag], $result); This would combine the existing item and the new item together to make an array $repeated _tag_index[$tag. '_' . $level] = 2; if (Isset ($current [$tag. ' _attr ')) {//The attribute of the last (0th) tag must is moved as well $current [$tag] [' 0_attr '] = $current [$t AG. ' _attr ']; Unset ($current [$tag. ' _attr ']); }} $last _item_index = $repeated _tag_index[$tag. '_' . $level]-1; $current =& $current [$tag] [$last _item_index]; }} elseif ($type = = "complete") {//Tags, ends in 1 line ' <tag/> '//See if the key is already taken . if (!isset ($current [$tag])) {//New Key $current [$tag] = $result; $repeated _tag_index[$tag. '_' . $level] = 1; if ($priority = = ' Tag ' and $attributes _data) $current [$tag. ' _attr '] = $attributes _data; } else {//if taken, put all things inside a list (array) If (Isset ($current [$tag][0]) and Is_array ($current [$tag]) ) {//If It is a already an array ...//... push the new element into the. Array. $current [$tag] [$repeated _tag_index[$tag. '_' . $level]] = $result; if ($priority = = ' Tag ' and $get _attributes and $attributes _data) {$current [$tag] [$repeated _tag_index[$tag. '_' . $level]. ' _attr '] = $attributes _data; } $repeated _tag_index[$tag. '_' . $level]++; } else {//If It is isn't an array ... $current [$tag] = aRray ($current [$tag], $result); //... Make it an array using using the existing value and the new value $repeated _tag_index[$tag. '_' . $level] = 1; if ($priority = = ' Tag ' and $get _attributes) {if (Isset ($current [$tag. ' _attr ')) {//The attribute of the last (0th) tag must is moved as well $current [$tag] [' 0_attr '] = $current [ $tag. ' _attr ']; Unset ($current [$tag. ' _attr ']); if ($attributes _data) {$current [$tag] [$repeated _tag_index[$tag. '_' . $level]. ' _attr '] = $attributes _data; }} $repeated _tag_index[$tag. '_' . $level]++; 0 and 1 index is already taken}}} elseif ($type = = ' close ') {//End of Tag ' </tag> ' $cu rrent = & $parent [$level-1]; }} return ($xml _array);} Array to Xmlclass array2xml {public $output = "<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\n"; Public $sub _item = Array (); Public function__construct ($array) {$sub _item = array (); $this->output. = $this->xmlmake ($array); } Public Function Xmlmake ($array, $FK = ') {$xml = '; Global $sub _item; foreach ($array as $key = + $value) {if (Is_array ($value)) {if (Is_numeric ($key)) {$this->sub _item=array_merge ($this->sub_item,array ($FK)); $xml. = "<{$fk}>". $this->xmlmake ($value, $key). "</{$fk}>"; } else {$xml. = "<{$key}>". $this->xmlmake ($value, $key). "</{$key}>"; }} else {$xml. = "<{$key}>{$value}</{$key}>\n"; }} return $xml; The Public function output () {foreach ($this->sub_item as $t) {$this->output = str_replace ("<{$t}><{ $t}> "," <{$t}> ", $this->output); $this->output = Str_replace ("</{$t}></{$t}>", "</{$t}>", $this->output); } return $this->output; }}
Summary : The above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
How to get the User IP address in PHP
Problems related to PHP garbage collection mechanism
PHP variables and date processing cases