dsnhalq37505
dsnhalq37505
2016-11-25 10:16

解析xml数据并使用foreach循环显示它

as i am using this code getting blank page,my code is as below

Please check the given link i want to retrive data from xml tag ,how could i retrive return-solution values from xml.

PHP

<?php
$xml = simplexml_load_file("result.xml");
foreach($xml->xm as $xm) {
    $totalfare = $xm->{'return-solutions'}->solution->{'pricing-summary'}-> {'total-fare'}. "<br>";
    echo $totalfare;
}
?>

xml is as follow:- there are so many index under tag, and so many index under but return-solutions comes after completion of onward-solutions

XML

<?xml version="1.0" encoding="UTF-8"?>    
<air-search-result>
    <onward-solutions>
        <solution index="1">
            <pricing-summary>
                <base-fare>2230.0</base-fare>
                <markup>10.0</markup>
                <discount>-25.0</discount>
                <taxes>1583.0</taxes>
                <total-fare>3798</total-fare>
            </pricing-summary>
            <flights>
                <flight>
                    <segments>
                        <segment>
                            <index>1</index>
                            <departure-airport>DEL</departure-airport>
                            <departure-terminal>3</departure-terminal>
                            <arrival-airport>BOM</arrival-airport>
                            <arrival-terminal>2</arrival-terminal>
                            <departure-date-time>2016-11-29T11:30:00</departure-date-time>
                            <arrival-date-time>2016-11-29T13:40:00</arrival-date-time>
                            <flight-number>316</flight-number>
                            <airline>9W</airline>
                            <operating-airline>9W</operating-airline>
                            <stops>0</stops>
                            <equipment>73H</equipment>
                            <duration>7800</duration>
                        </segment>
                    </segments>
                </flight>
            </flights>
            <pax-pricing-info-list>
                <pax-pricing-info>
                    <pax-type>ADT</pax-type>
                    <pricing-info-list>
                        <pricing-info>
                            <index>1</index>
                            <fare-basis-code>O2IPOD</fare-basis-code>
                            <fare-key>supp_AMADEUS|si-api-2526e960-7e7e-4ce3-bf32-0843b23c2833|fk_retail_9W_316_1480399200000_O2IPOD_true__fpr_3798</fare-key>
                            <fare-type>Refundable</fare-type>
                            <fare-subtype></fare-subtype>
                            <pricing-elements>
                                <pricing-element>
                                    <category>BF</category>
                                    <amount>2230.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>YQ</code>
                                    <amount>700.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>YR</code>
                                    <amount>50.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>UDF</code>
                                    <amount>564.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>JN</code>
                                    <amount>111.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>PSF</code>
                                    <amount>150.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>AIRLINE-MSC</code>
                                    <amount>4.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>AIRLINE-MSC</code>
                                    <amount>4.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>CUTE</code>
                                    <amount>0.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>OCT</code>
                                    <amount>0.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>DIS</category>
                                    <amount>-25.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>MKP</category>
                                    <amount>10.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>CLEARTRIP-SVC</code>
                                    <amount>0.0</amount>
                                </pricing-element>
                            </pricing-elements>
                        </pricing-info>
                    </pricing-info-list>
                    <booking-info-list>
                        <booking-info>
                            <index>1</index>
                            <segment-index>1</segment-index>
                            <pricing-info-index>1</pricing-info-index>
                            <booking-class>O</booking-class>
                            <cabin-type>E</cabin-type>
                            <ticket-type>E</ticket-type>
                            <external-references>
                                <external-reference>
                                    <name>AMD_VC_DEL_BOM_ADT</name>
                                    <value>9W</value>
                                </external-reference>
                                <external-reference>
                                    <name>tm-ignore-cacheExpiryTime</name>
                                    <value>1480152504475</value>
                                </external-reference>
                                <external-reference>
                                    <name>tm-ignore-cachePopulatedTime</name>
                                    <value>1480066104475</value>
                                </external-reference>
                                <external-reference>
                                    <name>AMD_BP_DEL_BOM_ADT</name>
                                    <value>Y</value>
                                </external-reference>
                            </external-references>
                        </booking-info>
                    </booking-info-list>
                </pax-pricing-info>
            </pax-pricing-info-list>
        </solution>
    </onward-solutions>
    <return-solutions>
        <solution index="1">
            <pricing-summary>
                <base-fare>1530.0</base-fare>
                <markup>10.0</markup>
                <discount>-25.0</discount>
                <taxes>1653.0</taxes>
                <total-fare>3168</total-fare>
            </pricing-summary>
            <flights>
                <flight>
                    <segments>
                        <segment>
                        <index>1</index>
                        <departure-airport>BOM</departure-airport>
                        <departure-terminal>2</departure-terminal>
                        <arrival-airport>DEL</arrival-airport>
                        <arrival-terminal>3</arrival-terminal>
                        <departure-date-time>2016-12-29T11:00:00</departure-date-time>
                        <arrival-date-time>2016-12-29T13:10:00</arrival-date-time>
                        <flight-number>307</flight-number>
                        <airline>9W</airline>
                        <operating-airline>9W</operating-airline>
                        <stops>0</stops>
                        <equipment>73H</equipment>
                        <duration>7800</duration>
                        </segment>
                    </segments>
                </flight>
            </flights>
            <pax-pricing-info-list>
                <pax-pricing-info>
                    <pax-type>ADT</pax-type>
                    <pricing-info-list>
                        <pricing-info>
                            <index>1</index>
                            <fare-basis-code>O2IPOD</fare-basis-code>
                            <fare-key>supp_AMADEUS|si-api-56b28b75-bc04-4dee-975e-e9c90e3b0391|fk_retail_9W_307_1482989400000_O2IPOD_true__fpr_3168</fare-key>
                            <fare-type>Refundable</fare-type>
                            <fare-subtype></fare-subtype>
                            <pricing-elements>
                                <pricing-element>
                                    <category>BF</category>
                                    <amount>1530.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>YQ</code>
                                    <amount>700.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>YR</code>
                                    <amount>50.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>JN</code>
                                    <amount>128.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>OCT</code>
                                    <amount>138.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>UDF</code>
                                    <amount>477.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>PSF</code>
                                    <amount>150.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>AIRLINE-MSC</code>
                                    <amount>5.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>AIRLINE-MSC</code>
                                    <amount>5.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>CUTE</code>
                                    <amount>0.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>DIS</category>
                                    <amount>-25.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>MKP</category>
                                    <amount>10.0</amount>
                                </pricing-element>
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>CLEARTRIP-SVC</code>
                                    <amount>0.0</amount>
                                </pricing-element>
                            </pricing-elements>
                        </pricing-info>
                    </pricing-info-list>
                    <booking-info-list>
                        <booking-info>
                            <index>1</index>
                            <segment-index>1</segment-index>
                            <pricing-info-index>1</pricing-info-index>
                            <booking-class>O</booking-class>
                            <cabin-type>E</cabin-type>
                            <ticket-type>E</ticket-type>
                            <external-references>
                                <external-reference>
                                    <name>AMD_BP_BOM_DEL_ADT</name>
                                    <value>Y</value>
                                </external-reference>
                                <external-reference>
                                    <name>tm-ignore-cacheExpiryTime</name>
                                    <value>1480144206652</value>
                                </external-reference>
                                <external-reference>
                                    <name>tm-ignore-cachePopulatedTime</name>
                                    <value>1480057806652</value>
                                </external-reference>
                                <external-reference>
                                    <name>AMD_VC_BOM_DEL_ADT</name>
                                    <value>9W</value>
                                </external-reference>
                            </external-references>
                        </booking-info>
                    </booking-info-list>
                </pax-pricing-info>
            </pax-pricing-info-list>
        </solution>
    </return-solutions>
</air-search-result>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongxing2030 dongxing2030 5年前
    <?php 
    $rslt = file_put_contents('file.xml', $result);
    $xm = new simpleXMLElement($result);
    foreach($xm as $row)
    { 
    echo $row->getName(). ":" .$row. "<br>";
    }
    ?>
    

    here in this code you can get onward as well as return solutions data

    点赞 评论 复制链接分享