header("Content-type:text/xml;charset=utf-8"); function getRootPath(){ $biasNum = substr_count($_SERVER['PHP_SELF'], '/'); //用'/'分割当前路径字符串,并计算分割后的字符串数量 $relativePath = './'; //初始化变量$relativePath为'./' for ($i = 0; $i < ($biasNum - 1); $i ++) { //循环添加'../' $relativePath .= '../'; } return $relativePath; } function display_children($pid,$level,$root,$temDom){ $targetRoot=$temDom->getElementsByTagName('root')->item(0); foreach ($root->childNodes as $sons){ $sonID=str_replace('n', '', $sons->nodeName); $sonPid=$sons->getElementsByTagName('pid')->item(0)->nodeValue; $sonName=$sons->getElementsByTagName('name')->item(0)->nodeValue; $sonExplain=$sons->getElementsByTagName('explain')->item(0)->nodeValue; if($sonPid==$pid){ //echo str_repeat('+', $level).$sonName.' and '.$sonID.'<br/>'; if($level==1){ $level1=$temDom->createElement('level1'); $level1->setAttribute('aid', $sonID); $level1->setAttribute('name', $sonName); $level1->setAttribute('explain', $sonExplain); $targetRoot->appendChild($level1); //$level1->getAttribute(''); }else{ $parsentLevel=$temDom->getElementsByTagName('level'.($level-1)); foreach ($parsentLevel as $levels){ if($levels->getAttribute('aid')==$sonPid){ $sonLevel=$temDom->createElement('level'.$level); $sonLevel->setAttribute('aid', $sonID); $sonLevel->setAttribute('name', $sonName); $sonLevel->setAttribute('explain', $sonExplain); $levels->appendChild($sonLevel); } } } display_children($sonID,$level+1,$root,$temDom); } } } $dom=new DOMDocument('1.0','utf-8'); $dom->load(getRootPath().'mydata/area/index/main.xml'); $root=$dom->getElementsByTagName('root')->item(0); $temDom=new DOMDocument('1.0','utf-8'); $root1=$temDom->createElement('root'); $temDom->appendChild($root1); //$tartRoot=$temDom->getElementsByTagName('root')->item(0); display_children(0, 1, $root,&$temDom); echo $temDom->saveXML();
转换后的树形结构如下
<?xml version="1.0" encoding="utf-8"?> <root><level1 aid="42" name="新加坡" explain="花园国度"/><level1 aid="41" name="俄罗斯" explain="北极熊"/><level1 aid="40" name="韩国" explain="高丽棒子"/><level1 aid="39" name="日本" explain="小日本"/><level1 aid="38" name="美国" explain="霸权主义国家"/><level1 aid="37" name="中国" explain="伟大的国家"><level2 aid="66" name="河南省" explain=""><level3 aid="80" name="开封" explain="少林所在地"/><level3 aid="79" name="洛阳" explain="历史名城"/><level3 aid="78" name="郑州市" explain="河南的省会"><level4 aid="81" name="测试区" explain=""/></level3></level2><level2 aid="55" name="贵州省" explain=""/><level2 aid="54" name="海南省" explain=""/><level2 aid="53" name="西藏自治区" explain=""/><level2 aid="52" name="内蒙古自治区" explain=""/><level2 aid="51" name="宁夏回族自治区" explain=""/><level2 aid="50" name="甘肃省" explain=""/><level2 aid="49" name="云南省" explain=""/><level2 aid="48" name="广西壮族自治区" explain=""/><level2 aid="47" name="广东省" explain=""/><level2 aid="46" name="陕西省" explain=""/><level2 aid="45" name="山西省" explain=""/><level2 aid="44" name="湖南省" explain=""/><level2 aid="43" name="湖北省" explain=""><level3 aid="70" name="枣阳市" explain=""/><level3 aid="61" name="孝感市" explain=""/><level3 aid="60" name="十堰市" explain=""/><level3 aid="59" name="荆州市" explain=""/><level3 aid="58" name="鄂州市" explain=""/><level3 aid="57" name="襄阳市" explain=""><level4 aid="68" name="襄城区" explain=""/><level4 aid="67" name="谷城县" explain=""/></level3><level3 aid="56" name="武汉市" explain=""><level4 aid="86" name="徐东区" explain="我去过"/><level4 aid="85" name="蔡甸区" explain=""/><level4 aid="84" name="江汉区" explain=""/><level4 aid="83" name="江岸区" explain=""/><level4 aid="82" name="青山区" explain=""/><level4 aid="69" name="洪山区" explain=""/><level4 aid="63" name="江汉区" explain=""/></level3></level2></level1></root>
<?xml version="1.0" encoding="utf-8"?> <root><n86><name><![CDATA[徐东区]]></name><pid><![CDATA[56]]></pid><explain><![CDATA[我去过]]></explain></n86><n85><name><![CDATA[蔡甸区]]></name><pid><![CDATA[56]]></pid><explain><![CDATA[]]></explain></n85><n84><name><![CDATA[江汉区]]></name><pid><![CDATA[56]]></pid><explain><![CDATA[]]></explain></n84><n83><name><![CDATA[江岸区]]></name><pid><![CDATA[56]]></pid><explain><![CDATA[]]></explain></n83><n82><name><![CDATA[青山区]]></name><pid><![CDATA[56]]></pid><explain><![CDATA[]]></explain></n82><n81><name><![CDATA[测试区]]></name><pid><![CDATA[78]]></pid><explain><![CDATA[]]></explain></n81><n80><name><![CDATA[开封]]></name><pid><![CDATA[66]]></pid><explain><![CDATA[少林所在地]]></explain></n80><n79><name><![CDATA[洛阳]]></name><pid><![CDATA[66]]></pid><explain><![CDATA[历史名城]]></explain></n79><n78><name><![CDATA[郑州市]]></name><pid><![CDATA[66]]></pid><explain><![CDATA[河南的省会]]></explain></n78><n70><name><![CDATA[枣阳市]]></name><pid><![CDATA[43]]></pid><explain><![CDATA[]]></explain></n70><n69><name><![CDATA[洪山区]]></name><pid><![CDATA[56]]></pid><explain><![CDATA[]]></explain></n69><n68><name><![CDATA[襄城区]]></name><pid><![CDATA[57]]></pid><explain><![CDATA[]]></explain></n68><n67><name><![CDATA[谷城县]]></name><pid><![CDATA[57]]></pid><explain><![CDATA[]]></explain></n67><n66><name><![CDATA[河南省]]></name><pid><![CDATA[37]]></pid><explain><![CDATA[]]></explain></n66><n63><name><![CDATA[江汉区]]></name><pid><![CDATA[56]]></pid><explain><![CDATA[]]></explain></n63><n61><name><![CDATA[孝感市]]></name><pid><![CDATA[43]]></pid><explain><![CDATA[]]></explain></n61><n60><name><![CDATA[十堰市]]></name><pid><![CDATA[43]]></pid><explain><![CDATA[]]></explain></n60><n59><name><![CDATA[荆州市]]></name><pid><![CDATA[43]]></pid><explain><![CDATA[]]></explain></n59><n58><name><![CDATA[鄂州市]]></name><pid><![CDATA[43]]></pid><explain><![CDATA[]]></explain></n58><n57><name><![CDATA[襄阳市]]></name><pid><![CDATA[43]]></pid><explain><![CDATA[]]></explain></n57><n56><name><![CDATA[武汉市]]></name><pid><![CDATA[43]]></pid><explain><![CDATA[]]></explain></n56><n55><name><![CDATA[贵州省]]></name><pid><![CDATA[37]]></pid><explain><![CDATA[]]></explain></n55><n54><name><![CDATA[海南省]]></name><pid><![CDATA[37]]></pid><explain><![CDATA[]]></explain></n54><n53><name><![CDATA[西藏自治区]]></name><pid><![CDATA[37]]></pid><explain><![CDATA[]]></explain></n53><n52><name><![CDATA[内蒙古自治区]]></name><pid><![CDATA[37]]></pid><explain><![CDATA[]]></explain></n52><n51><name><![CDATA[宁夏回族自治区]]></name><pid><![CDATA[37]]></pid><explain><![CDATA[]]></explain></n51><n50><name><![CDATA[甘肃省]]></name><pid><![CDATA[37]]></pid><explain><![CDATA[]]></explain></n50><n49><name><![CDATA[云南省]]></name><pid><![CDATA[37]]></pid><explain><![CDATA[]]></explain></n49><n48><name><![CDATA[广西壮族自治区]]></name><pid><![CDATA[37]]></pid><explain><![CDATA[]]></explain></n48><n47><name><![CDATA[广东省]]></name><pid><![CDATA[37]]></pid><explain><![CDATA[]]></explain></n47><n46><name><![CDATA[陕西省]]></name><pid><![CDATA[37]]></pid><explain><![CDATA[]]></explain></n46><n45><name><![CDATA[山西省]]></name><pid><![CDATA[37]]></pid><explain><![CDATA[]]></explain></n45><n44><name><![CDATA[湖南省]]></name><pid><![CDATA[37]]></pid><explain><![CDATA[]]></explain></n44><n43><name><![CDATA[湖北省]]></name><pid><![CDATA[37]]></pid><explain><![CDATA[]]></explain></n43><n42><name><![CDATA[新加坡]]></name><pid><![CDATA[0]]></pid><explain><![CDATA[花园国度]]></explain></n42><n41><name><![CDATA[俄罗斯]]></name><pid><![CDATA[0]]></pid><explain><![CDATA[北极熊]]></explain></n41><n40><name><![CDATA[韩国]]></name><pid><![CDATA[0]]></pid><explain><![CDATA[高丽棒子]]></explain></n40><n39><name><![CDATA[日本]]></name><pid><![CDATA[0]]></pid><explain><![CDATA[小日本]]></explain></n39><n38><name><![CDATA[美国]]></name><pid><![CDATA[0]]></pid><explain><![CDATA[霸权主义国家]]></explain></n38><n37><name><![CDATA[中国]]></name><pid><![CDATA[0]]></pid><explain><![CDATA[伟大的国家]]></explain></n37></root>
优点支持无限极分类,但是访问接口时返回XML树形结构,更利于程序读取!