PHP function Description and examples

Source: Internet
Author: User
Tags xml parser

/**

* 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& nbsp
        //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 a 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 is another element with the same tag name

if (Isset ($current [$tag][0])) {//if There is a 0th element it's 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
$current [$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 [$tag. ' _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/& gt; ' &NBSP
            //see If the key is already taken. &NBSP
            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 already an array ...

The.. 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) {2881064151
$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> '
$current = & $parent [$level-1];
}
}

Return ($xml _array);
}
?>


function description and examples

$arr = Xml2array (file_get_contents ("Tools.xml"), 1, ' attribute ');


Query keywords

PHP function Description and examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.