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

时间:2016-11-25 10:16:34

标签: php xml foreach xml-parsing

因为我使用此代码获取空白页面,我的代码如下

请检查我想要从xml标签中检索数据的给定链接,如何从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如下: - 标签下面有很多索引,并且在完成向前解决方案之后会有很多索引,但是返回解决方案

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 个答案:

答案 0 :(得分:0)

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

在此代码中,您可以继续前进以及返回解决方案数据