使用PHP数组解析XML数据

时间:2017-03-03 12:51:09

标签: php arrays xml parsing simplexml

我有一些包含酒店数据的XML文件。我需要解析这些文件中的酒店数据并保存到PHP数组中。必填字段是酒店节点和城市的brandId,brandName,name,address等,Location节点的地址。我正在使用SimpleXML PHP扩展。但它并没有为我工作。

是否还有其他好的PHP脚本来执行此任务。或者我如何更新我当前的脚本。我在下面提供了我的示例XML。

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

我的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>";

此脚本产生&#34;错误:无法创建对象&#34;。

请帮忙。谢谢。

2 个答案:

答案 0 :(得分:2)

您的代码中存在几个问题:

  • simplexml_load_file('newyork.xml') or die("Error: Cannot create object")是一种Perl语法,但这在PHP中是错误的。 PHP手册中解释了处理SimpleXML错误的方法:http://fr.php.net/manual/en/simplexml.examples-errors.php
  • $hotels->hotel没有描述从根元素开始的路径,直到Hotel元素Hotel元素不是根元素的子元素)< / em>的。另外,案件很重要。
  • 您需要使用方法SimpleXMLElement::attributes来访问元素属性。
  • 额外的困难,你必须处理命名空间。

您可以这样重写代码:

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

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

请注意,在这种情况下使用SimpleXML并不简单。实际上,DOMDocument可以直接访问Hotel个节点(通过DOMDocument::getElementsByTagName)并且不关心命名空间:

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

甚至XMLReader也更容易使用:

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

要完成,如果你有几个具有相同格式的XML文件转换为html,那么使用XSLT会很有趣。

答案 1 :(得分:-1)

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

我们可以像这样使用DOMDocument和php。并且你可以将$ dom作为具有所有值的对象。