自定义了一个php解析xml的接口,话不多说直接上代码
<?php
header("Content-type: text/html; charset=utf-8");
$ars=array();
$url='./test.xml';
function getChilds($node){
global $ars;
$arr1=array();
if($node->nodeType==3){
echo "<li>".$node->nodeValue."</li>";
}
else{
if($node->attributes->length>0) {
foreach ($node->attributes as $attr){
$arr['tag']=$node->nodeName;
$arr['name']=$attr->name;
$arr['value']=$attr->value;
array_push($arr1,$arr);
}
}
foreach ($node->childNodes as $child) {
getChilds($child);
}
array_push($ars,$arr1);
}
}
$doc=new DOMDocument();
$doc->preserveWhiteSpace=false;
$doc->load($url);
$root=$doc->documentElement;
getChilds($root);
var_dump($ars);
|