Rumah >pembangunan bahagian belakang >tutorial php >如何遍历XML文件
怎么遍历xml文件 需要放进数据库里面吗?
我想遍历后在前端的效果是以这种效果的
怎么显示就自己组织吧
$s = <<<EOF<?xml version="1.0" encoding="utf-8"?><MedicalDiagnosis dataBlock="Root0"> <Evaluations total="2"> <Evaluation data="植物系统"> <OBJECT data="苹果,梨">水果</OBJECT> <OBJECT data="竹子,富贵竹">乔木</OBJECT> <OBJECT data="菊花,康乃馨">花类</OBJECT> <OBJECT data="">其他</OBJECT> </Evaluation> <Evaluation data="动物系统"> <OBJECT data="老虎,狮子">陆</OBJECT> <OBJECT data="鱼,海马">海</OBJECT> <OBJECT data="">其他</OBJECT> </Evaluation> </Evaluations></MedicalDiagnosis>EOF;$x = simplexml_load_string($s);$data = [];foreach($x->Evaluations as $Evaluations){ foreach($Evaluations->Evaluation as $Evaluation){ $tmpData = current($Evaluation->attributes()->data); if(!isset($data["$tmpData"])){ $data["$tmpData"] = []; } foreach($Evaluation->OBJECT as $OBJECT){ $data["$tmpData"]["$OBJECT"] = current($OBJECT->attributes()->data); } }}print_r($data);/*Array( [植物系统] => Array ( [水果] => 苹果,梨 [乔木] => 竹子,富贵竹 [花类] => 菊花,康乃馨 [其他] => ) [动物系统] => Array ( [陆] => 老虎,狮子 [海] => 鱼,海马 [其他] => ))*/
怎么显示就自己组织吧
$s = <<<EOF<?xml version="1.0" encoding="utf-8"?><MedicalDiagnosis dataBlock="Root0"> <Evaluations total="2"> <Evaluation data="植物系统"> <OBJECT data="苹果,梨">水果</OBJECT> <OBJECT data="竹子,富贵竹">乔木</OBJECT> <OBJECT data="菊花,康乃馨">花类</OBJECT> <OBJECT data="">其他</OBJECT> </Evaluation> <Evaluation data="动物系统"> <OBJECT data="老虎,狮子">陆</OBJECT> <OBJECT data="鱼,海马">海</OBJECT> <OBJECT data="">其他</OBJECT> </Evaluation> </Evaluations></MedicalDiagnosis>EOF;$x = simplexml_load_string($s);$data = [];foreach($x->Evaluations as $Evaluations){ foreach($Evaluations->Evaluation as $Evaluation){ $tmpData = current($Evaluation->attributes()->data); if(!isset($data["$tmpData"])){ $data["$tmpData"] = []; } foreach($Evaluation->OBJECT as $OBJECT){ $data["$tmpData"]["$OBJECT"] = current($OBJECT->attributes()->data); } }}print_r($data);/*Array( [植物系统] => Array ( [水果] => 苹果,梨 [乔木] => 竹子,富贵竹 [花类] => 菊花,康乃馨 [其他] => ) [动物系统] => Array ( [陆] => 老虎,狮子 [海] => 鱼,海马 [其他] => ))*/
找不到网页....那是你服务器的错误吧,或者链接写错了...
找不到网页....那是你服务器的错误吧,或者链接写错了...
[] 是 php 5.4+ 的写法
分配到前台
$this->assign('data',$data);
怎么循环显示,要加什么样式 ...就是你的事了
http://document.thinkphp.cn/manual_3_2.html#volist