This article illustrates the conversion classes of PHP implementation XML and data format. Share to everyone for your reference. 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 as the the tag values-this results in a different array structure
In the return value. * $priority-can be ' tag ' or ' attribute '. This'll change the way the resulting array sturcture.
For ' tag ', the tags are given more importance. * Return:the parsed XML in an array form.
Use Print_r () to 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 have 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'll be turned to an array foreach ($xml _values as $data) {unset ($attributes, $val UE); Remove existing values, or there'll is trouble//This command would extract these variables into the foreachScope//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 are in the ' Attribute ' mode}//Set the attributes too. if (Isset ($attributes) and $get _attributes) {foreach ($attributes as $attr => $val) {if ($priority = = ' t
Ag ') $attributes _data[$attr] = $val; else $result [' attr '] [$attr] = $val;
Set all the attributes in a array called ' attr '}//* The 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 The element 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 an array if multiple tags with the same name appear together $curre nt[$tag] = Array ($current [$tag], $result); This would combine the existing item and the new item together to make a array $repeated _tag_index[$tag. '_' .
$level] = 2; if (Isset $current [$tag. ' _attr ']) {//The attribute of the last (0th) tag must is moved as $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 that ends in 1 line ' <tag/> '//= If the key is already tak
En.
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 a array ...//... push the new element into that 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 $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> '
$current = & $parent [$level-1];
} return ($xml _array); }//Array to XML class 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);
The 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}>&L T
{$t}> "," <{$t}> ", $this->output); $this->output = Str_replace ("</{$t}></{$t}> "," </{$t}> ", $this->output);
return $this->output; }
}
I hope this article will help you with your PHP programming.