今天我似乎無法解析我的xml。我做錯了什麼?我希望有人能指點我一個更好的/工作的解決方案。php DOM xml解析geon awry
我想解析巴士路線的XML文件看起來像這樣:
<buses>
<bus>
<num>12</num>
<stime>05:00</stime>
<etime>22:00</etime>
<freq>?</freq>
<route>
<stop>Eonyang Bus Terminal</stop>
<stop>Chuk Hyub</stop>
<stop>Eonyang Bolim Hospital</stop>
<stop>Samnammyeong Residential Center</stop>
<stop>SinAn</stop>
</route>
</bus>
<bus>
<num>13</num>
<stime>?</stime>
<etime>?</etime>
<freq>?</freq>
<route>
<stop>KTX Ulsan Station</stop>
<stop>Driving Hagwon</stop>
<stop>Jayeon Science High School</stop>
<stop>Eonyang Bus Stop</stop>
<stop>Bolim Hospital</stop>
<stop>Daewon Green Apt.</stop>
</route>
基於始發地和目的地的用戶輸入,我想通過站名搜索找到所需的總線或總線,但簡單地解析這個列表是行不通的。
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("buses-new.xml");
$buses=$xmlDoc->getElementsByTagName('bus');
for($i=0; $i<($buses->length); $i++) {
$route=$buses->item($i)->getElementsByTagName('route');
$busnum=$buses->item($i)->getElementsByTagName('num');
foreach($route->childNodes as $stop) {
$loc = $stop->nodeValue;
if (stristr($loc,$orig)) {
// found a bus with origin
$origins[$ocount]= $i;
$ocount++;
}
if (stristr($loc,$dest)) {
// found a bus with destination
$destination[$dcount] = $i;
$dcount++;
}
} // for all stops
} // for all buses
我得到一個錯誤---- foreach($ route-> childNodes as $ stop) - 在無效參數中說。
什麼是正確的方法來解析從每個路線不確定的孩子節點列表?
謝謝,這有效。 – Martin