simplexml_load_string 把 xml 串转化为字符串
<?php
$string = <<<XML//声明 xml
文档
<?xml version=’1.0 ?>
′
<document>
<title>Forty What?</title>
<from>Joe</from>
<to>Jane</to>
<body>
I know that’s the answer — but what’s the question?
</body>
</document>
XML;
$xml = simplexml_load_string($string);
var_dump($xml);
?>
This script will display:
SimpleXMLElement Object
(
[title] => Forty What?
[from] => Joe
[to] => Jane
[body] =>
I know that’s the answer — but what’s the question?
)
如果想把 xml 转换为数组,先转换为字符串含有键值的字符串,然后再进行循环就可以
成为数组了,
/**
* xml
转换为数组
* @param unknown_type $xml
*/
private function xml_to_array($xml)
{
$array = (array)(simplexml_load_string($xml,’SimpleXMLElement’,
LIBXML_NOCDATA));
foreach ($array as $key=>$item){
$array[$key] = $this->struct_to_array((array)$item);
}
return $array;
}