2011-06-29 71 views
2

我遇到問題,試圖解析我從另一個站點使用SimpleXML提取的XML文件。我需要顯示某些房產的租金。但是,我只想顯示某些房產的租金。我從來沒有見過這種結構方式的XML文件:http://dl.dropbox.com/u/1925679/example.xmlPHP:使用SimpleXML解析XML

首先,我怎麼會通過這個文件的解析基於

Property->PropertyID->MITS:Identification->MITS:PrimaryID 

然後,回聲出基於ID的Property->Floorplan->MarketRent(Min and Max)

TIA !!!!

+0

你是否被命名空間(「MITS:」)拋出?如果是這樣,不要擔心它們,只要將整個事件(「MITS:CompanyInfo」)作爲節點名稱即可。 –

回答

2
// get all properties 
$properties = $xml->xpath('//Property'); 

// get document namesapces in prefix => uri format 
$namespaces = $xml->getNamespaces(true); 


foreach($properies as $property) 
{ 
    // get namespaced nodes 
    $identification = $property->PropertyID->children($namespaces['MITS']); 
    $id = $identification->children($namespaces['MITS'])->PrimaryID; 

    // do your check id is an expected value or whatever and if so then... 

    foreach($property->Floorplan as $floorplan){ 
    { 
     echo $floorplan->MarketRent['min']; 
     echo $floorplan->MarketRent['max']; 
    } 
} 

你很可能COM了XPath查詢,只選擇性能與給定的ID或一組ID的直線距離,那麼你將只需要遍歷它們並調用佈局規劃環,但生病留給你的調查:-)我會說,如果你走這條路線,你將需要用我認爲xpath註冊命名空間。很確定那不是自動的。

+1

這工作完美!我必須做的唯一改變是$ xml-> getNamespaces(true);謝謝!!! – csm232s

+0

沒問題!很高興你解決了它。將更新答案與您調整他人的好處:-) – prodigitalson