我有XML格式的记录。我有10个不同的xml记录。现在我想要php函数动态读取XML记录。
我创建了方法,但它适用于单一类型的xml。我的代码是:
function readXMLDoc($xml_file){
$xmlDoc = new DOMDocument();
$res = $xmlDoc->load('uploads/xml_records/'.$xml_file);
if (!$res) {
echo("Failed to parse XML\n");
return false;
}
$events = $xmlDoc->getElementsByTagName("photoSet");
$data= array();
foreach($events as $key=>$event):
$dates = $event->getElementsByTagName("desc");
$data[$key]['desc'] = $dates->item(0)->nodeValue;
$venues = $event->getElementsByTagName("set");
$data[$key]['set'] = $venues->item(0)->nodeValue;
$names = $event->getElementsByTagName("number");
$data[$key]['number'] = $names->item(0)->nodeValue;
$names = $event->getElementsByTagName("thumb");
$data[$key]['thumb'] = $names->item(0)->nodeValue;
endforeach;
return $data;
}
我想动态地获取节点名称和值。