Search code examples
phparraysxmlparsingsimplexml

Parsing XML data using into PHP array


I have some XML files containing hotel data. I need to parse hotel data from these files and save to PHP array. Required fields are brandId, brandName, name, address etc from Hotel node and city, address from Location node. I was using SimpleXML PHP extenstion. But it didn't working for me.

Is there any other good PHP scripts for doing this task. Or how can i update my current script. I have provided my sample XML below.

<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>

My PHP script:

$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>";

This script prodcuing "Error: Cannot create object".

Please help. Thank you.


Solution

  • There are several problems in your code:

    • simplexml_load_file('newyork.xml') or die("Error: Cannot create object") is a kind of Perl syntax but that is wrong in PHP. A way to deal with SimpleXML errors is explained in the PHP manual: http://fr.php.net/manual/en/simplexml.examples-errors.php
    • $hotels->hotel doesn't describe the path from the root element until the Hotel elements (Hotel elements aren't children of the root element). As an aside, the case matters.
    • You need to use the method SimpleXMLElement::attributes to have an access to an element attributes.
    • Additional difficulty, you have to deal with namespaces.

    You can rewrite your code this way:

    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']);
        }    
    }
    

    or using 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']);
        }   
    }
    

    Note that it is paradoxically less simple to use SimpleXML in this case. Indeed, DOMDocument gives a direct access to Hotel nodes (via DOMDocument::getElementsByTagName) and doesn't care about the namespaces:

    $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'));
    }
    

    Even XMLReader is more easy to use:

    $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();
    

    To finish, if you have several XML files with the same format to transform to html, it can be interesting to use XSLT.