duangua6912 2013-05-07 20:18
浏览 31
已采纳

来自XML部分的PHP输出游览信息[关闭]

I'm receiving an touristic XML document which is structured like this:

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

I need to output each tour like this:

City name | Hotel name | Room type | Food type | Start Date | Duration | Price

My current PHP code is like this:

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

This works, but unfortunately it outputs only separate XML values.

To get the whole structure properly I need to associate the non-constant value "routeId=xxx" in <tourGroup> with the same value in <route id="xxx"> from which I can then take the "id" in <place placeId="xxx"> and output the "name" from <places><place id="xxx" name="...">, and then do the same for <hotelId="xxx"> and output the "name" from <hotels><hotel id="xxx" name="..."> and so on for <roomId> and <foodId>. I hope that makes a bit of sense.

Unfortunately I just can't get my head around this and my PHP knowledge is pretty basic, but if someone can point me in the right direction, I'd really appreciate it. Thank you!

  • 写回答

2条回答 默认 最新

  • doubi4617 2013-05-07 23:47
    关注

    A little heads-up for starters, your XML is not well formed as it's missing the closing </msResult> root tag.

    XML is far from being an optimal solution for what you want, my advice would be to store this info in a relational database and work from there with simple queries.

    Nevertheless I've coded up a solution just for the fun of it, so you can see how XPath can help you relate data between the nodes:

    <?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
    ";
        }
    }
    

    Output:

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

报告相同问题?

悬赏问题

  • ¥15 关于#Mybatis#的问题:怎么办重复数据如下"KSDM":37,"KSMC":"呼吸内科门诊","ksdm":37,"ksmc":"呼吸内科门诊"xml文件填写
  • ¥40 servlet的web程序部署出错
  • ¥50 activiti 新建流程系列问题
  • ¥50 为什么我版本升级之后运行速度变慢5倍??
  • ¥15 如何在gazebo中加载机械臂和机械手
  • ¥15 纯运放实现隔离采样方案设计
  • ¥20 easyconnect无法连接后缀带.com的网站,一直显示找不到服务器ip地址,但是带有数字的网站又是可以连接上的,如何解决
  • ¥15 电脑开机过商标后就直接这样,求解各位
  • ¥15 mysql , 用自己创建的本地主机和用户名 登录不上
  • ¥15 关于#web项目#的问题,请各位专家解答!