我使用API來顯示結果w.r.t以搜索查詢。使用PHP讀取XML屬性
輸出將採用XML形式,我需要解析XML並在瀏覽器中顯示結果。
該文件包含許多屬性,我不知道如何提取結果。
這裏是我的代碼:
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns3:tourDetailsFullResponse xmlns:ns2="http://example.com/" xmlns:ns3="http://example.com/" xmlns:ns4="http://www.example.com/">
<ns4:MarketLocalisedTourData SellingCompanyCode="CODE1" OperatingProductCode="CODE2" MarketVariationCode="CODE3" Duration="8" Currency="USD" CatalogueCode="CODE4" TropicsBrochureCode="CODE5" BrandCode="CODE6" BookableOnline="true">
<ns4:TourInfo>
<ns4:TourName>London to Rome Highlights 2013</ns4:TourName>
<ns4:Metadata>
<ns4:TourCategories>
<ns4:TourCategory Name="TourStyles">
<ns4:CategoryValue Name="European Discoveries"/>
</ns4:TourCategory>
</ns4:TourCategories>
<ns4:Brochures>
<ns4:Brochure Name="CostSaver 2013" Code="CODE1"/>
</ns4:Brochures>
</ns4:Metadata>
<ns4:ContinentsVisited>
<ns4:Continent Name="Europe" Code="EURO"/>
</ns4:ContinentsVisited>
<ns4:CountriesVisited>
<ns4:Country Name="France" Continent="EURO" Code="FR"/>
<ns4:Country Name="United Kingdom" Continent="EURO" Code="GB"/>
<ns4:Country Name="Italy" Continent="EURO" Code="IT"/>
<ns4:Country Name="Switzerland" Continent="EURO" Code="CH"/>
</ns4:CountriesVisited>
<ns4:LocationsVisited>
<ns4:Location Name="Paris" Country="FR"/>
<ns4:Location Name="Dover" Country="GB"/>
<ns4:Location Name="London" Country="GB"/>
<ns4:Location Name="Rome" Country="IT"/>
<ns4:Location Name="Calais" Country="FR"/>
<ns4:Location Name="Venice" Country="IT"/>
<ns4:Location Name="Lucerne" Country="CH"/>
<ns4:Location Name="Florence" Country="IT"/>
</ns4:LocationsVisited>
<ns4:Description>A lively journey through elegant Paris, gorgeous Lake Lucerne, charming Venice and cultural Florence, bookended by stays in London and legendary Rome. Spend a day at leisure in London and get the best views of Rome from high up in St. Peter’s Basilica before throwing a lucky coin in Trevi Fountain.</ns4:Description>
<ns4:Assets>
<ns4:Image Width="297" Url="http://www.example.com/imageurl" Type="map" Name="route_map" Height="297" Caption="route_map"/>
<ns4:Image Width="600" Url="http://www.example.com/imageurl" Type="map" Name="route_map" Height="600" Caption="route_map"/>
<ns4:Image Width="531" Url="http://www.example.com/imageurl" Type="photo" Name="primary_image" Height="531" Caption="primary_image"/>
<ns4:Image Width="125" Url="http://www.example.com/imageurl" Type="photo" Name="primary_image" Height="125" Caption="primary_image"/>
</ns4:Assets>
....continues
現在我想提取大陸參觀,訪問的國家,走訪地點,行程,租入資產價值等等
我試圖用簡單的XML像下面
$feed = "response1_full.xml";
$xml = simplexml_load_file($feed);
print_r($xml);
foreach($xml->LocationsVisited[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
also tried
foreach($xml->TourInfo->LocationsVisited[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
也嘗試導致同樣的錯誤
所有其他的可能性Fatal error: Call to a member function attributes() on a non-object in C:\xampp\htdocs\test\xmlread2.php on line 10
我在這兩天以上尋找解決方案。
您尋找的解決方案意味着您瞭解XML中的命名空間以及[如何使用simplexml訪問命名空間元素](http://stackoverflow.com/search?q=how+to+access+namespaced+elements+與+ simplexml的)。所以請環顧四周,看看你的問題是否曾被問過。提問並回答你自己的問題也是可以的。 – hakre 2013-03-05 14:11:11