PHP输出来自XML部分的游览信息

时间:2013-05-07 20:18:01

标签: php xml xpath

我收到的旅游XML文档的结构如下:

<msResult timestamp="20130507153907">
    <places>
        <place id="1000008" name="Germany" placePath="1000008"/>
        <place id="1000591" name="Berlin" placePath="10000081000591"/>
    </places>
    <rooms>
        <room id="1002" name="Standard"/>
        <room id="1042" name="Standart"/>
    </rooms>
    <foods>
        <food id="2" name="BB" description="Breakfast"/>
        <food id="1" name="No" description="No food"/>
    </foods>
    <hotels>
        <hotel categoryId="6"/>
        <hotel id="9047" placeId="1000591" name="BERLIN EXCELSIOR" categoryId="8" desc="en"/>
        <hotel id="37803" placeId="1000591" name="MARK APART" categoryId="6" desc="en"/>
     </hotels>
     <routes>
        <route id="223534">
            <point>
                <place placeId="1000591" hotelId="37803" categoryId="6" foodId="1" tourTypeIds="1" roomId="1002"/>
            </point>
         </route>
         <route id="223535">
             <point>
                 <place placeId="1000591" hotelId="9047" categoryId="8" foodId="1" tourTypeIds="1" roomId="1042"/>
             </point>
         </route>
    </routes>
    <tours>
        <group departureId="64" currencyId="2" transportIds="1" includeIds="1 2 32" include="description" exclude="" comment="" program="" transport="">
            <tourGroup duration="3" routeId="223534" statusId="3" tourTypeIds="1" dates="17.05.2013">
                <tour accomId="6" price="850" ids="1339755026"/>
            </tourGroup>
        </group>
        <group departureId="64" currencyId="2" transportIds="1" includeIds="1 2 32" include="descriptiin" exclude="" comment="" program="" transport="">
            <tourGroup duration="3" routeId="223534" statusId="3" tourTypeIds="1" dates="31.05.2013">
                <tour accomId="6" price="902" ids="1339755024"/>
            </tourGroup>
        </group>
        <group departureId="64" currencyId="2" transportIds="1" includeIds="1 2 32" include="description" exclude="" comment="" program="" transport="">
            <tourGroup duration="3" routeId="223535" statusId="3" tourTypeIds="1" dates="17.05.2013">
                <tour accomId="6" price="981" ids="1339755027"/>
            </tourGroup>
        </group>
    </tours>
</msResult>

我需要像这样输出每个巡演:

城市名称|酒店名称|房间类型|食物类型|开始日期|持续时间|价格

我目前的PHP代码是这样的:

$xml = simplexml_load_string($out);

foreach ($xml->places->place as $place) {
?>
<h5><?php echo $place["name"]; ?> - 
<?php
}

foreach ($xml->tours->group as $group) {
?>
Date: <?php echo $group->tourGroup["dates"]; ?> - 
Days: <?php echo $group->tourGroup["duration"]; ?> - 
Price: $<?php echo $group->tourGroup->tour["price"];
}

这很有效,但不幸的是它只输出单独的XML值。

为了正确地获得整个结构,我需要将"routeId=xxx"中的非常数值<tourGroup><route id="xxx">中的相同值相关联,然后我可以从"id"获取<place placeId="xxx">"name"中输出<places>来自<place id="xxx" name="..."> <hotelId="xxx">,然后对"name"执行相同操作,并从{{1}输出<hotels> } <hotel id="xxx" name="...">等等<roomId><foodId>。我希望这有点道理。

不幸的是,我无法理解这一点,我的PHP知识非常基础,但如果有人能指出我正确的方向,我真的很感激。谢谢!

2 个答案:

答案 0 :(得分:1)

  

对于初学者而言,您的XML不是well formed,因为它缺少结束</msResult>根标记。

XML远非成为您想要的最佳解决方案,我的建议是将此信息存储在关系数据库中,并通过简单查询从那里开始工作。

尽管如此,我为了它的乐趣编写了一个解决方案,因此您可以看到XPath如何帮助您在节点之间关联数据:

<?php
    $xml = <<<XML
<msResult timestamp="20130507153907">
    <places>
        <place id="1000008" name="Germany" placePath="1000008"/>
        <place id="1000591" name="Berlin" placePath="10000081000591"/>
    </places>
    <rooms>
        <room id="1002" name="Standard"/>
        <room id="1042" name="Standart"/>
    </rooms>
    <foods>
        <food id="2" name="BB" description="Breakfast"/>
        <food id="1" name="No" description="No food"/>
    </foods>
    <hotels>
        <hotel categoryId="6"/>
        <hotel id="9047" placeId="1000591" name="BERLIN EXCELSIOR" categoryId="8" desc="en"/>
        <hotel id="37803" placeId="1000591" name="MARK APART" categoryId="6" desc="en"/>
     </hotels>
     <routes>
        <route id="223534">
            <point>
                <place placeId="1000591" hotelId="37803" categoryId="6" foodId="1" tourTypeIds="1" roomId="1002"/>
            </point>
         </route>
         <route id="223535">
             <point>
                 <place placeId="1000591" hotelId="9047" categoryId="8" foodId="1" tourTypeIds="1" roomId="1042"/>
             </point>
         </route>
    </routes>
    <tours>
        <group departureId="64" currencyId="2" transportIds="1" includeIds="1 2 32" include="description" exclude="" comment="" program="" transport="">
            <tourGroup duration="3" routeId="223534" statusId="3" tourTypeIds="1" dates="17.05.2013">
                <tour accomId="6" price="850" ids="1339755026"/>
            </tourGroup>
        </group>
        <group departureId="64" currencyId="2" transportIds="1" includeIds="1 2 32" include="descriptiin" exclude="" comment="" program="" transport="">
            <tourGroup duration="3" routeId="223534" statusId="3" tourTypeIds="1" dates="31.05.2013">
                <tour accomId="6" price="902" ids="1339755024"/>
            </tourGroup>
        </group>
        <group departureId="64" currencyId="2" transportIds="1" includeIds="1 2 32" include="description" exclude="" comment="" program="" transport="">
            <tourGroup duration="3" routeId="223535" statusId="3" tourTypeIds="1" dates="17.05.2013">
                <tour accomId="6" price="981" ids="1339755027"/>
            </tourGroup>
        </group>
    </tours>
</msResult>
XML;

$sxe = simplexml_load_string($xml);

// Iterate groups
foreach ($sxe->tours->group as $group) {

    // Iterate groups' tours
    foreach ($group->tourGroup as $tourGroup) {
        $routeId = (string) $tourGroup['routeId'];
        $placeId = $sxe->xpath("//route[@id={$routeId}]/point/place/@placeId");
        $placeId = (string) $placeId[0];

        // City name
        $cityName = $sxe->xpath("//place[@id={$placeId}]/@name");
        $cityName = $cityName[0];

        // Hotel name
        $hotelName = $sxe->xpath("//hotel[@id=//route[@id={$routeId}]/point/place/@hotelId]/@name");
        $hotelName = (string) $hotelName[0];

        // Food type
        $foodType = $sxe->xpath("//food[@id=//route[@id={$routeId}]/point/place/@foodId]/@description");
        $foodType = (string) $foodType[0];

        // Room name
        $roomName = $sxe->xpath("//room[@id=//route[@id={$routeId}]/point/place/@roomId]/@name");
        $roomName = (string) $roomName[0];

        $startDate = (string) $tourGroup['dates'];
        $duration  = (string) $tourGroup['duration'];
        $price     = (string) $tourGroup->tour['price'];

        echo "$cityName - $hotelName - $roomName - $foodType - $startDate - $duration - $price\n";
    }
}

输出:

Berlin - MARK APART - Standard - No food - 17.05.2013 - 3 - 850
Berlin - MARK APART - Standard - No food - 31.05.2013 - 3 - 902
Berlin - BERLIN EXCELSIOR - Standart - No food - 17.05.2013 - 3 - 981

答案 1 :(得分:0)

Arrgh。我的第一枪:

$xml = simplexml_load_string($x); // assume XML in $x

// building array $data containing place, room, food, hotel, route
foreach ($xml->places->place as $place)
$data['place'][(string)$place['id']] = (string)$place['name'];

foreach ($xml->rooms->room as $room)
$data['room'][(string)$room['id']] = (string)$room['name'];

foreach ($xml->foods->food as $food)
$data['food'][(string)$food['id']] = (string)$food['name'];

foreach ($xml->hotels->hotel as $hotel)
$data['hotel'][(string)$hotel['id']] = (string)$hotel['name'];

foreach ($xml->routes->route as $route)
    $data['route'][(string)$route['id']] = array(
        'placeId' => (string)$route->point->place['placeId'],
        'hotelId' => (string)$route->point->place['hotelId'],
        'foodId'  => (string)$route->point->place['foodId'],
        'roomId'  => (string)$route->point->place['roomId']);

// looping through <tourGroup> and echoing...
foreach ($xml->tours->group as $g) {
    $rid = (int)$g->tourGroup['routeId'];
    echo $data['place'][$data['route'][$rid]['placeId']] . " | ";
    echo $data['hotel'][$data['route'][$rid]['hotelId']] . " | ";
    echo $data['room'][$data['route'][$rid]['roomId']] . " | ";
    echo $data['food'][$data['route'][$rid]['foodId']] . " | ";
    echo $g->tourGroup['dates'] . " | " . $g->tourGroup['duration'] . " | ";
    echo $g->tourGroup->tour['price'] . "<br />";
}

看到它有效:http://codepad.viper-7.com/wwgTuE

输出:

Berlin | MARK APART | Standard | No | 17.05.2013 | 3 | 850
Berlin | MARK APART | Standard | No | 31.05.2013 | 3 | 902
Berlin | BERLIN EXCELSIOR | Standart | No | 17.05.2013 | 3 | 981