1,第一种
//XMLReader
function getWeather()
{
header("Content-type:text/html; Charset=utf-8");
$url = "http://www.guanw.com/Public/Home/test.xml";
// 加载XML内容
$z = new \XMLReader();
$z->open($url);
$doc = new \DOMDocument;
while ($z->read()) {
while ($z->name == 'product') {
$node = simplexml_import_dom($doc->importNode($z->expand(), true));
$jsonStr = json_encode($node);
$jsonArray = json_decode($jsonStr,true);
$arr[]= $jsonArray;
$z->next('product');
}
}
var_dump($arr);
2,第二种
//SimpleXmlElement
public function dealXml(){
$url = "http://www.guanw.com/Public/Home/test.xml";
$deadlines = array();
$xml = file_get_contents($url);
$sxe = new \SimpleXmlElement($xml);
$jsonStr = json_encode($sxe);
$jsonArray = json_decode($jsonStr,true);
var_dump($jsonArray);
die;
}