douzao2992 2013-03-04 07:47
浏览 126


I m using a API to display results w.r.t to search query.

The output will be in the form of XML, I need to parse the XML and display the result in the browser.

The file contains many attributes from which i m not sure how to extract the results.

Here is my code:

<?xml version="1.0" ?>
<S:Envelope xmlns:S="">
<ns3:tourDetailsFullResponse xmlns:ns2="" xmlns:ns3="" xmlns:ns4="">

<ns4:MarketLocalisedTourData SellingCompanyCode="CODE1" OperatingProductCode="CODE2" MarketVariationCode="CODE3" Duration="8" Currency="USD" CatalogueCode="CODE4" TropicsBrochureCode="CODE5" BrandCode="CODE6" BookableOnline="true">

<ns4:TourName>London to Rome Highlights 2013</ns4:TourName>
        <ns4:TourCategory Name="TourStyles">
            <ns4:CategoryValue Name="European Discoveries"/>
        <ns4:Brochure Name="CostSaver 2013" Code="CODE1"/>
    <ns4:Continent Name="Europe" Code="EURO"/>
    <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: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: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:Image Width="297" Url="" Type="map" Name="route_map" Height="297" Caption="route_map"/>
    <ns4:Image Width="600" Url="" Type="map" Name="route_map" Height="600" Caption="route_map"/>
    <ns4:Image Width="531" Url="" Type="photo" Name="primary_image" Height="531" Caption="primary_image"/>
    <ns4:Image Width="125" Url="" Type="photo" Name="primary_image" Height="125" Caption="primary_image"/>

Now i want to extract the continents visited , countries visited, locations visited, itinerary, assests etc...

I tried using simple xml like the following

$feed = "response1_full.xml";

$xml = simplexml_load_file($feed);


foreach($xml->LocationsVisited[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"
also tried 
foreach($xml->TourInfo->LocationsVisited[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"

also tried all other possibilities which results in same error

Fatal error: Call to a member function attributes() on a non-object in C:\xampp\htdocs\test\xmlread2.php on line 10

I m searching a solution for this more than couple of days.

  • 写回答

3条回答 默认 最新

  • dozug64282 2013-03-04 08:36

    If you want to traverse into children in their own namespace you have to tell SimpleXMLElement which namespace:

    $ns4 = $xml->children('S', true)->Body
            ->children('ns3', true)->tourDetailsFullResponse
                ->children('ns4', true);
    $locations = $ns4->MarketLocalisedTourData->TourInfo->LocationsVisited->Location;
    foreach ($locations as $location) {
        $attributes = $location->attributes();
        echo $attributes['Name'];
        echo $attributes['Country'];

    However you can make your life more easy by using xpath with the namespace(s) you're interested in by registering them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?
  • ¥15 如何用QDomDocument读取内容为空格的xml数据
  • ¥15 请阅读下面代码,帮我修改下代码