2017-03-03 2 views
0

J'ai des fichiers XML contenant des données d'hôtels. J'ai besoin d'analyser les données de l'hôtel à partir de ces fichiers et de les enregistrer dans un tableau PHP. Les champs obligatoires sont brandId, brandName, nom, adresse etc. du noeud et de la ville de l'hôtel, adresse du noeud Location. J'utilisais l'extension PHP SimpleXML. Mais ça n'a pas marché pour moi.Analyse de données XML en utilisant un tableau PHP

Existe-t-il d'autres bons scripts PHP pour effectuer cette tâche? Ou comment puis-je mettre à jour mon script actuel. J'ai fourni mon exemple XML ci-dessous.

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <SearchHotelsResponse xmlns="http://example.com/webservices/hotelhds"> 
     <SearchHotelsResult> 
      <Info version="7.123" culture="en-US" serverTime="2017-03-01T05:20:08.9411324-05:00" xmlns="http://schemas.abc.com/webservices/hotelV5rggs"/> 
      <HotelList xmlns="http://schemas.abc.com/webservices/hotelrshhslkmnj"> 
       <Hotel brandId="424" brandName="Independent Managed Hotels" category="Deluxe" PropertySubType="" NumOfRoom="899" minAverPublishPrice="345.16" PropertyType="Hotel" name="Lotte New York Palace - Demo" bestValue="false" hotelId="1040577" provider="localTgsProvider" thumb="http://image1.urlforimages.com/Images/1040577/100x100/1559480295.jpg" currency="USD" minAverPrice="345.16" starsLevel="4.5" desc="Demo - Location&#xD;&#xA;This modern luxury hotel is one of the top addresses in the country. It enjoys a superb midtown location on Madison Avenue, opposite St Patrick's Cathedral and directly at the heart of the city. Countless shops and entertainment venues, b"> 
        <Location countryCode="US" stateCode="NY" city="New York" searchingState="New York" searchingCity="New York" location="Manhattan - Midtown" address="Demo - 455 Madison Avenue New York New York 10022" longitude="-73.9753354497376" latitude="40.758002144334"/> 
        <RoomTypes> 
         <RoomType NumOfBathrooms="0" roomTypeCategory="Superior" roomTypeCategoryId="20" nights="7" name="Superior" productId="1040577;2754674;302661" startDate="2017-03-20T00:00:00" hotelRoomTypeId="2754674" roomId="80" isAvailable="true"> 
          <AvailabilityBreakdown> 
           <Availability offset="0" status="true"/> 
           <Availability offset="1" status="true"/> 
           <Availability offset="2" status="true"/> 
           <Availability offset="3" status="true"/> 
           <Availability offset="4" status="true"/> 
           <Availability offset="5" status="true"/> 
           <Availability offset="6" status="true"/> 
          </AvailabilityBreakdown> 
          <Occupancies> 
           <Occupancy taxPublish="335.05" avrNightPublishPrice="345.16" occupId="1040577;2754674;302661;3;2;0" occupPrice="2416.1" tax="335.05" maxGuests="2" maxChild="0" avrNightPrice="345.16" occupPublishPrice="2416.1" isPublish="false" bedding="2,1"> 
            <PriceBreakdown> 
             <Price offset="0" value="408.38" valuePublish="408.38"/> 
             <Price offset="1" value="408.38" valuePublish="408.38"/> 
             <Price offset="2" value="408.38" valuePublish="408.38"/> 
             <Price offset="3" value="408.38" valuePublish="408.38"/> 
             <Price offset="4" value="260.86" valuePublish="260.86"/> 
             <Price offset="5" value="260.86" valuePublish="260.86"/> 
             <Price offset="6" value="260.86" valuePublish="260.86"/> 
            </PriceBreakdown> 
            <Rooms> 
             <Room seqNum="1"> 
              <AdultNum>2</AdultNum> 
              <ChildNum>0</ChildNum> 
             </Room> 
            </Rooms> 
            <SelctedSupplements/> 
            <BoardBases/> 
           </Occupancy> 
          </Occupancies> 
         </RoomType> 
         <RoomType NumOfBathrooms="0" roomTypeCategory="Standard" roomTypeCategoryId="17" nights="7" name="Catheral View" productId="1040577;1165849;302661" startDate="2017-03-20T00:00:00" hotelRoomTypeId="1165849" roomId="4768962" isAvailable="true"> 
          <AvailabilityBreakdown> 
           <Availability offset="0" status="true"/> 
           <Availability offset="1" status="true"/> 
           <Availability offset="2" status="true"/> 
           <Availability offset="3" status="true"/> 
           <Availability offset="4" status="true"/> 
           <Availability offset="5" status="true"/> 
           <Availability offset="6" status="true"/> 
          </AvailabilityBreakdown> 
          <Occupancies> 
           <Occupancy taxPublish="378.77" avrNightPublishPrice="393.71" occupId="1040577;1165849;302661;3;2;0" occupPrice="2755.94" tax="378.77" maxGuests="2" maxChild="0" avrNightPrice="393.71" occupPublishPrice="2755.94" isPublish="false" bedding="2,1"> 
            <PriceBreakdown> 
             <Price offset="0" value="463.7" valuePublish="463.7"/> 
             <Price offset="1" value="463.7" valuePublish="463.7"/> 
             <Price offset="2" value="463.7" valuePublish="463.7"/> 
             <Price offset="3" value="463.7" valuePublish="463.7"/> 
             <Price offset="4" value="300.38" valuePublish="300.38"/> 
             <Price offset="5" value="300.38" valuePublish="300.38"/> 
             <Price offset="6" value="300.38" valuePublish="300.38"/> 
            </PriceBreakdown> 
            <Rooms> 
             <Room seqNum="1"> 
              <AdultNum>2</AdultNum> 
              <ChildNum>0</ChildNum> 
             </Room> 
            </Rooms> 
            <SelctedSupplements/> 
            <BoardBases/> 
           </Occupancy> 
          </Occupancies> 
         </RoomType> 
         <RoomType NumOfBathrooms="0" roomTypeCategory="Standard" roomTypeCategoryId="17" nights="7" name="Tower King" productId="1040577;4014667;302661" startDate="2017-03-20T00:00:00" hotelRoomTypeId="4014667" roomId="2203455" isAvailable="true"> 
          <AvailabilityBreakdown> 
           <Availability offset="0" status="true"/> 
           <Availability offset="1" status="true"/> 
           <Availability offset="2" status="true"/> 
           <Availability offset="3" status="true"/> 
           <Availability offset="4" status="true"/> 
           <Availability offset="5" status="true"/> 
           <Availability offset="6" status="true"/> 
          </AvailabilityBreakdown> 
          <Occupancies> 
           <Occupancy taxPublish="553.49" avrNightPublishPrice="587.89" occupId="1040577;4014667;302661;3;2;0" occupPrice="4115.21" tax="553.49" maxGuests="2" maxChild="0" avrNightPrice="587.89" occupPublishPrice="4115.21" isPublish="false" bedding="2,1"> 
            <PriceBreakdown> 
             <Price offset="0" value="684.98" valuePublish="684.98"/> 
             <Price offset="1" value="684.98" valuePublish="684.98"/> 
             <Price offset="2" value="684.98" valuePublish="684.98"/> 
             <Price offset="3" value="684.98" valuePublish="684.98"/> 
             <Price offset="4" value="458.43" valuePublish="458.43"/> 
             <Price offset="5" value="458.43" valuePublish="458.43"/> 
             <Price offset="6" value="458.43" valuePublish="458.43"/> 
            </PriceBreakdown> 
            <Rooms> 
             <Room seqNum="1"> 
              <AdultNum>2</AdultNum> 
              <ChildNum>0</ChildNum> 
             </Room> 
            </Rooms> 
            <SelctedSupplements/> 
            <BoardBases/> 
           </Occupancy> 
          </Occupancies> 
         </RoomType> 
         <RoomType NumOfBathrooms="0" roomTypeCategory="Suite" roomTypeCategoryId="19" nights="7" name="Tower Executive Suite" productId="1040577;13147974;302661" startDate="2017-03-20T00:00:00" hotelRoomTypeId="13147974" roomId="4830397" isAvailable="true"> 
          <AvailabilityBreakdown> 
           <Availability offset="0" status="true"/> 
           <Availability offset="1" status="true"/> 
           <Availability offset="2" status="true"/> 
           <Availability offset="3" status="true"/> 
           <Availability offset="4" status="true"/> 
           <Availability offset="5" status="true"/> 
           <Availability offset="6" status="true"/> 
          </AvailabilityBreakdown> 
          <Occupancies> 
           <Occupancy taxPublish="651.49" avrNightPublishPrice="696.84" occupId="1040577;13147974;302661;3;2;0" occupPrice="4877.88" tax="651.49" maxGuests="2" maxChild="0" avrNightPrice="696.84" occupPublishPrice="4877.88" isPublish="false" bedding="2,1"> 
            <PriceBreakdown> 
             <Price offset="0" value="696.84" valuePublish="696.84"/> 
             <Price offset="1" value="696.84" valuePublish="696.84"/> 
             <Price offset="2" value="696.84" valuePublish="696.84"/> 
             <Price offset="3" value="696.84" valuePublish="696.84"/> 
             <Price offset="4" value="696.84" valuePublish="696.84"/> 
             <Price offset="5" value="696.84" valuePublish="696.84"/> 
             <Price offset="6" value="696.84" valuePublish="696.84"/> 
            </PriceBreakdown> 
            <Rooms> 
             <Room seqNum="1"> 
              <AdultNum>2</AdultNum> 
              <ChildNum>0</ChildNum> 
             </Room> 
            </Rooms> 
            <SelctedSupplements/> 
            <BoardBases/> 
           </Occupancy> 
          </Occupancies> 
         </RoomType> 
         <RoomType NumOfBathrooms="0" roomTypeCategory="Suite" roomTypeCategoryId="19" nights="7" name="Tower Corner Suite" productId="1040577;13147975;302661" startDate="2017-03-20T00:00:00" hotelRoomTypeId="13147975" roomId="4830398" isAvailable="true"> 
          <AvailabilityBreakdown> 
           <Availability offset="0" status="true"/> 
           <Availability offset="1" status="true"/> 
           <Availability offset="2" status="true"/> 
           <Availability offset="3" status="true"/> 
           <Availability offset="4" status="true"/> 
           <Availability offset="5" status="true"/> 
           <Availability offset="6" status="true"/> 
          </AvailabilityBreakdown> 
          <Occupancies> 
           <Occupancy taxPublish="1004.49" avrNightPublishPrice="1089.16" occupId="1040577;13147975;302661;3;2;0" occupPrice="7624.11" tax="1004.49" maxGuests="2" maxChild="0" avrNightPrice="1089.16" occupPublishPrice="7624.11" isPublish="false" bedding="2,1"> 
            <PriceBreakdown> 
             <Price offset="0" value="1111.74" valuePublish="1111.74"/> 
             <Price offset="1" value="1111.74" valuePublish="1111.74"/> 
             <Price offset="2" value="1111.74" valuePublish="1111.74"/> 
             <Price offset="3" value="1111.74" valuePublish="1111.74"/> 
             <Price offset="4" value="1059.05" valuePublish="1059.05"/> 
             <Price offset="5" value="1059.05" valuePublish="1059.05"/> 
             <Price offset="6" value="1059.05" valuePublish="1059.05"/> 
            </PriceBreakdown> 
            <Rooms> 
             <Room seqNum="1"> 
              <AdultNum>2</AdultNum> 
              <ChildNum>0</ChildNum> 
             </Room> 
            </Rooms> 
            <SelctedSupplements/> 
            <BoardBases/> 
           </Occupancy> 
          </Occupancies> 
         </RoomType> 
        </RoomTypes> 
       </Hotel> 
       <Hotel brandId="288" brandName="Holiday Inn" category="First Class" PropertySubType="" NumOfRoom="200" minAverPublishPrice="202.77" PropertyType="Hotel" name="Holiday Inn NYC Times Square - Demo" bestValue="true" hotelId="1437966" provider="localTgsProvider" thumb="http://image1.urlforimages.com/Images/1437966/100x100/8012H66O018.JPG" currency="USD" minAverPrice="202.77" starsLevel="3.5" desc="Demo - "> 
        <Location countryCode="US" stateCode="NY" city="New York" searchingState="New York" searchingCity="New York" location="Manhattan - Times Square/Broadway" address="Demo - 585 8th Avenue New York New York 10018" longitude="-73.991712" latitude="40.755229"/> 
        <RoomTypes> 
         <RoomType NumOfBathrooms="0" roomTypeCategory="Standard" roomTypeCategoryId="17" nights="7" name="Standard 2 Beds" productId="1437966;14787188;12927607" startDate="2017-03-20T00:00:00" hotelRoomTypeId="14787188" roomId="1286" isAvailable="true"> 
          <AvailabilityBreakdown> 
           <Availability offset="0" status="true"/> 
           <Availability offset="1" status="true"/> 
           <Availability offset="2" status="true"/> 
           <Availability offset="3" status="true"/> 
           <Availability offset="4" status="true"/> 
           <Availability offset="5" status="true"/> 
           <Availability offset="6" status="true"/> 
          </AvailabilityBreakdown> 
          <Discount xsi:type="ProgressivePromotion" from="2017-03-20T00:00:00" to="2017-03-27T00:00:00" type="Percent" value="10" name="Special Deal"/> 
          <Occupancies> 
           <Occupancy taxPublish="208.76" avrNightPublishPrice="202.77" occupId="1437966;14787188;12927607;2;2;0;2" occupPrice="1419.4" tax="208.76" maxGuests="4" maxChild="2" avrNightPrice="202.77" occupPublishPrice="1419.4" isPublish="false" bedding="2,2"> 
            <PriceBreakdown> 
             <Price offset="0" value="207.86" valuePublish="207.86"/> 
             <Price offset="1" value="220.58" valuePublish="220.58"/> 
             <Price offset="2" value="207.86" valuePublish="207.86"/> 
             <Price offset="3" value="195.14" valuePublish="195.14"/> 
             <Price offset="4" value="195.14" valuePublish="195.14"/> 
             <Price offset="5" value="220.58" valuePublish="220.58"/> 
             <Price offset="6" value="172.24" valuePublish="172.24"/> 
            </PriceBreakdown> 
            <Rooms> 
             <Room seqNum="1"> 
              <AdultNum>2</AdultNum> 
              <ChildNum>0</ChildNum> 
             </Room> 
            </Rooms> 
            <SelctedSupplements> 
             <Supplement xsi:type="PerRoomSupplement" suppId="1200127" suppName="2 tickets for Empire State Building " supptType="1" suppIsMandatory="false" suppChargeType="Addition" price="52.85" publishPrice="52.85"/> 
            </SelctedSupplements> 
            <BoardBases> 
             <Boardbase bbId="1" bbName="Continental Breakfast" bbPrice="356.21" bbPublishPrice="356.21"/> 
            </BoardBases> 
           </Occupancy> 
          </Occupancies> 
         </RoomType> 
        </RoomTypes> 
       </Hotel> 
       <Hotel brandId="51" brandName="Embassy Suites Hotels" category="First Class" PropertySubType="" NumOfRoom="189" minAverPublishPrice="198.19" PropertyType="Hotel" name="Embassy Suites Elizabeth - Newark Airport - Demo" bestValue="false" hotelId="1343771" provider="localTgsProvider" thumb="http://image1.urlforimages.com/Images/1343771/100x100/1629572161.jpg" currency="USD" minAverPrice="198.19" starsLevel="3.5" desc="Demo - Our straightforward suites include separate living rooms with pull-out sofas, microwaves, minifridges and coffeemakers, plus 2 flat-screen TVs, and WiFi (fee).&#xD;&#xA;&#xD;&#xA;A free made-to-order breakfast is offered each day"> 
        <Location countryCode="US" stateCode="NJ" city="Elizabeth" searchingState="New Jersey" searchingCity="Elizabeth" location="Newark Liberty International Airport" address="Demo - 95 International Blvd Elizabeth New Jersey 07201" longitude="-74.1771680963125" latitude="40.6621379134249"/> 
        <RoomTypes> 
         <RoomType NumOfBathrooms="0" roomTypeCategory="Standard" roomTypeCategoryId="17" nights="7" name="Standard" productId="1343771;13479523;11755505" startDate="2017-03-20T00:00:00" hotelRoomTypeId="13479523" roomId="3" isAvailable="true"> 
          <AvailabilityBreakdown> 
           <Availability offset="0" status="true"/> 
           <Availability offset="1" status="true"/> 
           <Availability offset="2" status="true"/> 
           <Availability offset="3" status="true"/> 
           <Availability offset="4" status="true"/> 
           <Availability offset="5" status="true"/> 
           <Availability offset="6" status="true"/> 
          </AvailabilityBreakdown> 
          <Occupancies> 
           <Occupancy taxPublish="236.02" avrNightPublishPrice="198.19" occupId="1343771;13479523;11755505;2;2;0;1" occupPrice="1387.31" tax="236.02" maxGuests="4" maxChild="2" avrNightPrice="198.19" occupPublishPrice="1387.31" isPublish="false" bedding="2,1"> 
            <PriceBreakdown> 
             <Price offset="0" value="206.09" valuePublish="206.09"/> 
             <Price offset="1" value="206.09" valuePublish="206.09"/> 
             <Price offset="2" value="206.09" valuePublish="206.09"/> 
             <Price offset="3" value="206.09" valuePublish="206.09"/> 
             <Price offset="4" value="178.43" valuePublish="178.43"/> 
             <Price offset="5" value="178.43" valuePublish="178.43"/> 
             <Price offset="6" value="206.09" valuePublish="206.09"/> 
            </PriceBreakdown> 
            <Rooms> 
             <Room seqNum="1"> 
              <AdultNum>2</AdultNum> 
              <ChildNum>0</ChildNum> 
             </Room> 
            </Rooms> 
            <SelctedSupplements/> 
            <BoardBases> 
             <Boardbase bbId="4" bbName="Breakfast Buffet" bbPrice="0" bbPublishPrice="0"/> 
            </BoardBases> 
           </Occupancy> 
          </Occupancies> 
         </RoomType> 
         <RoomType NumOfBathrooms="0" roomTypeCategory="Standard" roomTypeCategoryId="17" nights="7" name="Standard Double" productId="1343771;15050057;11755505" startDate="2017-03-20T00:00:00" hotelRoomTypeId="15050057" roomId="932" isAvailable="true"> 
          <AvailabilityBreakdown> 
           <Availability offset="0" status="true"/> 
           <Availability offset="1" status="true"/> 
           <Availability offset="2" status="true"/> 
           <Availability offset="3" status="true"/> 
           <Availability offset="4" status="true"/> 
           <Availability offset="5" status="true"/> 
           <Availability offset="6" status="true"/> 
          </AvailabilityBreakdown> 
          <Occupancies> 
           <Occupancy taxPublish="236.02" avrNightPublishPrice="198.19" occupId="1343771;15050057;11755505;2;2;0;1" occupPrice="1387.31" tax="236.02" maxGuests="4" maxChild="0" avrNightPrice="198.19" occupPublishPrice="1387.31" isPublish="false" bedding="2,1"> 
            <PriceBreakdown> 
             <Price offset="0" value="206.09" valuePublish="206.09"/> 
             <Price offset="1" value="206.09" valuePublish="206.09"/> 
             <Price offset="2" value="206.09" valuePublish="206.09"/> 
             <Price offset="3" value="206.09" valuePublish="206.09"/> 
             <Price offset="4" value="178.43" valuePublish="178.43"/> 
             <Price offset="5" value="178.43" valuePublish="178.43"/> 
             <Price offset="6" value="206.09" valuePublish="206.09"/> 
            </PriceBreakdown> 
            <Rooms> 
             <Room seqNum="1"> 
              <AdultNum>2</AdultNum> 
              <ChildNum>0</ChildNum> 
             </Room> 
            </Rooms> 
            <SelctedSupplements/> 
            <BoardBases> 
             <Boardbase bbId="4" bbName="Breakfast Buffet" bbPrice="0" bbPublishPrice="0"/> 
            </BoardBases> 
           </Occupancy> 
           <Occupancy taxPublish="236.02" avrNightPublishPrice="198.19" occupId="1343771;15050057;11755505;2;2;0;2" occupPrice="1387.31" tax="236.02" maxGuests="4" maxChild="0" avrNightPrice="198.19" occupPublishPrice="1387.31" isPublish="false" bedding="2,2"> 
            <PriceBreakdown> 
             <Price offset="0" value="206.09" valuePublish="206.09"/> 
             <Price offset="1" value="206.09" valuePublish="206.09"/> 
             <Price offset="2" value="206.09" valuePublish="206.09"/> 
             <Price offset="3" value="206.09" valuePublish="206.09"/> 
             <Price offset="4" value="178.43" valuePublish="178.43"/> 
             <Price offset="5" value="178.43" valuePublish="178.43"/> 
             <Price offset="6" value="206.09" valuePublish="206.09"/> 
            </PriceBreakdown> 
            <Rooms> 
             <Room seqNum="1"> 
              <AdultNum>2</AdultNum> 
              <ChildNum>0</ChildNum> 
             </Room> 
            </Rooms> 
            <SelctedSupplements/> 
            <BoardBases> 
             <Boardbase bbId="4" bbName="Breakfast Buffet" bbPrice="0" bbPublishPrice="0"/> 
            </BoardBases> 
           </Occupancy> 
          </Occupancies> 
         </RoomType> 
        </RoomTypes> 
       </Hotel> 
</HotelList> 
     </SearchHotelsResult> 
    </SearchHotelsResponse> 
</s:Body> 

Mon script PHP:

$hotels = simplexml_load_file('newyork.xml') or die("Error: Cannot create object"); 
echo "<ul id='hotelList'>"; 
foreach ($hotels->hotel as $hotelinfo) 
{ 
    $title = $hotelinfo['name']; 
    $brandId = $hotelinfo['brandId']; 

    echo "Test1"; 
    //$artist=$hotelinfo->artist; 
    //$date=$hotelinfo['dateplayed']; 
    echo "<li><div class='title'>",$title,"</div><div class='artist'>by ",$brandId,"</div><time>",1111,"</time></li>"; 
} 
echo "</ul>"; 

Ce script prodcuing "Erreur: Impossible de créer l'objet".

Aidez-nous s'il vous plaît. Je vous remercie.

Répondre

2

Il y a plusieurs problèmes dans votre code:

  • simplexml_load_file('newyork.xml') or die("Error: Cannot create object") est une sorte de syntaxe Perl, mais qui ne va pas en PHP. Une façon de traiter les erreurs de SimpleXML est expliqué dans le manuel PHP: http://fr.php.net/manual/en/simplexml.examples-errors.php
  • $hotels->hotel ne décrit pas le chemin de l'élément racine jusqu'à ce que les Hotel éléments (Hotel éléments ne sont pas des enfants de l'élément racine). En aparté, l'affaire compte. Vous devez utiliser la méthode SimpleXMLElement::attributes pour avoir un accès aux attributs d'un élément.
  • Difficulté supplémentaire, vous devez gérer les espaces de noms.

Vous pouvez réécrire votre code de cette façon:

libxml_use_internal_errors(true); 
$sxe = simplexml_load_file('newyork.xml', 'SimpleXMLElement', 0, 's', true); 
if ($sxe === false) { 
    foreach(libxml_get_errors() as $error) { 
     echo $error->message, PHP_EOL; 
    } 
} else { 
    $hotels = $sxe -> Body[0] -> children("http://example.com/webservices/hotelhds") 
        -> SearchHotelsResponse[0] 
        -> SearchHotelsResult[0] -> children("http://schemas.abc.com/webservices/hotelrshhslkmnj") 
        -> HotelList[0] -> Hotel; 

    $rowF = '<li><div class="title">%s</div><div class="artist">by %s</div></li>'. PHP_EOL; 

    foreach ($hotels as $hotel) { 
     $attr = $hotel->attributes(); 
     printf($rowF, $attr['name'], $attr['brandId']); 
    }  
} 

ou en utilisant XPath:

libxml_use_internal_errors(true); 
$sxe = simplexml_load_file('newyork.xml'); 
if ($sxe === false) { 
    foreach(libxml_get_errors() as $error) { 
     echo $error->message . PHP_EOL; 
    } 
} else { 
    $sxe->registerXPathNamespace('h', 'http://schemas.abc.com/webservices/hotelrshhslkmnj'); 
    $hotels = $sxe->xpath('//h:Hotel'); 
    foreach ($hotels as $hotel) { 
     $attr = $hotel->attributes(); 
     printf($rowF, $attr['name'], $attr['brandId']); 
    } 
} 

Notez qu'il est paradoxalement moins simple à utiliser SimpleXML dans ce cas. En effet, DOMDocument donne un accès direct à Hotel nœuds (via DOMDocument::getElementsByTagName) et ne se soucie pas sur les espaces de noms:

$rowF = '<li><div class="title">%s</div><div class="artist">by %s</div></li>'. PHP_EOL; 

$dom = new DOMDocument; 
$dom->load('newyork.xml'); 

$hotelNodeList = $dom->getElementsByTagName('Hotel'); 

foreach ($hotelNodeList as $hotelNode) { 
    printf($rowF, $hotelNode->getAttribute('name'), $hotelNode->getAttribute('brandId')); 
} 

Même XMLReader est plus facile à utiliser:

$xml = new XMLReader(); 
$xml->open('newyork.xml'); 

while ($xml->read()) { 
    if ($xml->nodeType === XMLReader::ELEMENT && $xml->name == 'Hotel') { 
     do { 
      printf($rowF, $xml->getAttribute('name'), $xml->getAttribute('brandId')); 
     } while($xml->next('Hotel')); 
     break; 
    } 
} 
$xml->close(); 

Pour terminer, si vous avoir plusieurs fichiers XML avec le même format à transformer en html, il peut être intéressant d'utiliser XSLT.

+0

Fonctionne parfaitement! Merci beaucoup Casimir .. – shfkktm

+0

Si nous obtenons XML en réponse, comment pouvons-nous utiliser simplexml_load_string ($ response) ici au lieu de simplexml_load_file? – shfkktm

+0

@ user1693763: Vous n'avez rien à changer, remplacez simplexml_load_file' par simplexml_load_string avec les mêmes paramètres sauf le premier qui doit être cette fois la chaîne contenant votre xml. –

-1
<?php $dom = new DOMDocument(); 
$dom->loadXML('/path/to/xml.xml');?> 

nous pouvons utiliser DOMDocument avec php comme ceci. Et vous pouvez $ dom comme objet avec toutes les valeurs.

+0

Je viens d'essayer ça. Mais cette erreur d'affichage: Avertissement: DOMDocument :: loadXML(): Balise de démarrage attendue, '<' introuvable dans l'entité, ligne: 1 – shfkktm

+0

'DOMDocument :: loadXML' est à charger à partir d'une chaîne, pour charger le contenu XML à partir d'un URL, vous avez besoin de 'DOMDocument :: load' –

+0

Merci Casimir. – shfkktm