It was just yesterday that a similar demand was made ... A few lines of code can be done.
If you use curl to get the XML data
$xml = simplexml_load_string ($data);
$data [' tk '] = Json_decode (Json_encode ($xml), TRUE);
If you get the URL data directly,
$xml = Simplexml_load_file ($data);
$data [' tk '] = Json_decode (Json_encode ($xml), TRUE);
Convert the SimpleXML object to JSON first, and then convert the JSON to an array.
XML and Array transfer
2007-12-18 12:29:37| Category: Xml | Report | Font size Subscription
If garbled, change the header function
<?php//header (' content-type:text/html;charset=utf-8 ');
Class XmlArray
{
Private $xml = ';//variables for reading XML
private $data; //generated XML
private $dom _tree;//xml directory tree
/**
__construct only for reading XML
*/
function __construct ($xml = "")
{
if (empty ($xml))
{
return null;
}
//$this->xml = $xml;
Else
{
$this->loadxml ($xml);
}
}
/**
mount the XML document to be processed can also be loaded at initialization
*/
Public Function Loadxml ($filepath)
{
$handle = @fopen ($filepath, "R");
while (!feof ($handle) && $handle)
{
$xml _data. = Fgets ($handle, 4096);
}
$this->xml= $xml _data;
}
/**
Working with XML documents
*/
Private Function _struct_to_array ($values, & $i)
{
$child = Array ();
if (Isset ($values [$i] [' value ']) Array_push ($child, $values [$i] [' value ']);
while ($i + + < count ($values))
{
Switch ($values [$i] [' type '])
{
Case ' CDATA ':
Array_push ($child, $values [$i] [' value ']);
Break
Case ' complete ':
$name = $values [$i] [' tag '];
if (!empty ($name))
{
$child [$name]= ($values [$i] [' value '])? ($values [$i] [' value ']): ';
if (Isset ($values [$i] [' attributes ']))
{
$child [$name] = $values [$i] [' attributes '];
}
}
Break
Case ' open ':
$name = $values [$i] [' tag '];
$size = Isset ($child [$name])? sizeof ($child [$name]): 0;
$child [$name] [$size] = $this->_struct_to_array ($values, $i);
Break
Case ' close ':
return $child;
Break
}
}
return $child;
}
/**
Working with XML documents, actually calling _struct_to_array () processing
*/
Public Function Xml2array ()
{
$xml =& $this->xml;
$values = Array ();
$index = Array ();
$array = Array ();
$parser = Xml_parser_create ();
Xml_parser_set_option ($parser, Xml_option_skip_white, 1);
Xml_parser_set_option ($parser, xml_option_case_folding, 0);
Xml_parse_into_struct ($parser, $xml, $values, $index);
Xml_parser_free ($parser);
$i = 0;
$name = $values [$i] [' tag '];
$array [$name] = Isset ($values [$i] [' attributes '])? $values [$i] [' Attributes ']: ';
$array [$name] = $this->_struct_to_array ($values, $i);
return $array;
}
The following is the code that transforms an array into XML using the DOM
/**
Reading an array
*/
Public Function Array2xml ($array) {
if (!is_array ($array)) {
throw new Exception (' array2xml requires an array ', 1);
Unset ($array);
}
if (!count ($array)) {
throw new Exception (' array is empty ', 2);
Unset ($array);
}
$this->data = new DOMDocument ();
$this->dom_tree = $this->data->createelement (' result ');//default to label
$this->data->appendchild ($this->dom_tree);
$this->recurse_node ($array, $this->dom_tree);
}
/**
Working with arrays as XML
*/
Private Function Recurse_node ($data, $obj) {
$i = 0;
foreach ($data as $key = = $value) {
if (Is_array ($value)) {
Recurse if Neccisary
$sub _obj[$i] = $this->data->createelement ($key);//Create label
$obj->appendchild ($sub _obj[$i]); Add a label to the $obj tab
$this->recurse_node ($value, $sub _obj[$i]); Add values to this tab
} elseif (Is_object ($value)) {
No object just say what's it is
$sub _obj[$i] = $this->data->createelement ($key, ' Object: '. $key. ' "Type: '. Get_class ($value). ‘"‘);
$obj->appendchild ($sub _obj[$i]);
} else {
Straight up data, no weirdness
$sub _obj[$i] = $this->data->createelement ($key, $value);
If this is the last node, create the section name and content
$obj->appendchild ($sub _obj[$i]);
}
$i + +;
}
}
/**
To save an array as an XML file
*/
Public Function savexml ($arraytoxml = "")
{
if ($arraytoxml = = "")
{
$out = Iconv ("GBK", "Utf-8", "Enter the file name to be saved");
echo "<script>alert (' $out ') </script>";
}
Else
{
return $this->data->save ($arraytoxml);
}
}
}
?>
<?php
$test = Array (
' one ' + ' number ',
' both ' = > Array (
' C ' =>1,
' d ' =>2,
' E ' =>3,
' f ' = >4
),
' three ' = > ' alpha '
);
/* Create Object */
$XMLOBJ = new XmlArray ();
/* Convert */
$xml = $xmlObj->array2xml ($test);
/* Output Save */
$xmlObj->savexml ("A.xml");
Parsing XML into an array output
<?php
/* Create Object */
$XMLOBJ = new XmlArray ();
/* Load */
$xml = $XMLOBJ->loadxml ("A.xml");
/* Convert */
$data = $XMLOBJ->xml2array ();
/* Display */
Print_r ($data);
?>