0
我有XML格式的記錄。我有10個不同的xml記錄。現在我想PHP函數動態讀取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;
}
我想動態獲取節點名稱和值。
也許嘗試使用PHP的一些XML庫?(http://php.net/manual/en/book.simplexml.php) – 2012-04-02 09:34:51
或PHP的本地DOMDocument http://php.net/manual/en/class。 domdocument.php – 2012-04-02 09:36:13