如何将嵌套在simplexml对象中的数组转换为simplxml对象?

时间:2014-10-20 06:32:53

标签: php simplexml

我正在尝试从simplexml对象中提取一组数据。但是,我的代码返回一个通知,如:

Notice: Trying to get property of non-object in C:\wamp\www\Departeasy_php\xmlintegrate.php on line 19.         

我注意到的是,在xml对象中嵌套了一个数组,这可能导致它抛出这样的错误。

我经常搜索以找到解决方案,但无济于事。

请帮忙。

这是simplexml对象的一部分:

SimpleXMLElement Object
(
    [solution] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [pricing-summary] => SimpleXMLElement Object
                        (
                            [base-fare] => 2000.00
                            [taxes] => 2887.40
                            [total-fare] => 4887.40
                        )

                    [flights] => SimpleXMLElement Object
                        (
                            [flight] => SimpleXMLElement Object
                                (
                                    [segments] => SimpleXMLElement Object
                                        (
                                            [segment] => SimpleXMLElement Object
                                                (
                                                    [index] => 1
                                                    [departure-airport] => BOM
                                                    [arrival-airport] => CCU
                                                    [departure-date-time] => 2008-11-01T09:20:00
                                                    [arrival-date-time] => 2008-11-01T12:00:00
                                                    [airline] => 6E
                                                    [flight-number] => 321
                                                    [operating-airline] => 6E
                                                    [stops] => 0
                                                    [equipment] => 320
                                                    [duration] => 9600
                                                )

                                        )

                                )

                        )

                    [pax-pricing-info-list] => SimpleXMLElement Object
                        (
                            [pax-pricing-info] => SimpleXMLElement Object
                                (
                                    [pax-type] => ADT
                                    [pricing-info-list] => SimpleXMLElement Object
                                        (
                                            [pricing-info] => SimpleXMLElement Object
                                                (
                                                    [index] => 1
                                                    [fare-basis-code] => H0BOMCCU
                                                    [fare-key] => ZG9tZXN0aWMtNkUtUi1IMEJPTUNDVS0zNDMzNTc3MjUw
                                                    [pricing-elements] => SimpleXMLElement Object
                                                        (
                                                            [pricing-element] => Array
                                                                (
                                                                    [0] => SimpleXMLElement Object
                                                                        (
                                                                            [category] => TAX
                                                                            [code] => CLEARTRIP-SVC
                                                                            [amount] => 12.40
                                                                        )

                                                                    [1] => SimpleXMLElement Object
                                                                        (
                                                                            [category] => BF
                                                                            [amount] => 2000.00
                                                                        )

                                                                    [2] => SimpleXMLElement Object
                                                                        (
                                                                            [category] => TAX
                                                                            [code] => PSF
                                                                            [amount] => 225.00
                                                                        )

                                                                    [3] => SimpleXMLElement Object
                                                                        (
                                                                            [category] => TAX
                                                                            [code] => YQ
                                                                            [amount] => 2350.00
                                                                        )

                                                                    [4] => SimpleXMLElement Object
                                                                        (
                                                                            [category] => TAX
                                                                            [code] => YR
                                                                            [amount] => 150.00
                                                                        )

                                                                    [5] => SimpleXMLElement Object
                                                                        (
                                                                            [category] => TAX
                                                                            [code] => TF
                                                                            [amount] => 100.00
                                                                        )

                                                                    [6] => SimpleXMLElement Object
                                                                        (
                                                                            [category] => TAX
                                                                            [code] => AIRLINE-MSC
                                                                            [amount] => 50.00
                                                                        )

                                                                    [7] => SimpleXMLElement Object
                                                                        (
                                                                            [category] => MKP
                                                                            [amount] => 0.00
                                                                        )

                                                                    [8] => SimpleXMLElement Object
                                                                        (
                                                                            [category] => DIS
                                                                            [amount] => 0.00
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                    [booking-info-list] => SimpleXMLElement Object
                                        (
                                            [booking-info] => SimpleXMLElement Object
                                                (
                                                    [index] => 1
                                                    [segment-index] => 1
                                                    [pricing-info-index] => 1
                                                    [booking-class] => R
                                                    [cabin-type] => E
                                                    [ticket-type] => E
                                                )

                                        )

                                )

                        )

                )

这是我的代码:`

<?php 
//error_reporting(E_ALL & ~E_NOTICE);
$contents= simplexml_load_file('xml/cleartrip.xml');
$onward_flights = $contents->{"onward-solutions"}->children();
    foreach ($onward_flights as $onwardflights) 
        {
                //$onwardflights= $onwardflights;
                //$onwardflights = $contents->{"onward-solutions"}->children();
                //$main= $onwardflights->solution->flights->flight->segments->segment->children();
                $flightno= $onwardflights->solution->flights->flight->segments->segment->airline.'-'.$onwardflights->solution->flights->flight->segments->segment->{"flight-number"};
                $airline= $flightno;
                $depdate= substr($onwardflights->solution->flights->flight->segments->segment->{"departure-date-time"},0,10);
                $deptime= substr($onwardflights->solution->flights->flight->segments->segment->{"departure-date-time"},11,8);
                $arrdate= substr($onwardflights->solution->flights->flight->segments->segment->{"arrival-date-time"},0,10);
                $arrtime= substr($onwardflights->solution->flights->flight->segments->segment->{"arrival-date-time"},11,8);
                $duration= $onwardflights->solution->flights->flight->segments->segment->duration;
                $stops= $onwardflights->solution->flights->flight->segments->segment->stops;
                $origin= substr(filter_input(INPUT_POST, "origin"), 0, 3);
                $destination= substr(filter_input(INPUT_POST, "destination"), 0, 3);
                $fare= $onwardflights->solution->{"pricing-summary"}->{"total-fare"};
                echo " ".$flightno." ".$depdate." ".$arrdate." ".$fare." ".$airline." ".$deptime." ".$arrtime." ".$duration." ".$stops." ".$origin." ".$destination."<br/>";

    }

?>

`

示例xml:

    <?xml version="1.0" encoding="UTF-8"?>
-
<air-search-result
    xmlns="http://www.cleartrip.com/air/"> -
    <onward-solutions> -
        <solution> -
            <pricing-summary>
                <base-fare>2000.00</base-fare>
                <taxes>2887.40</taxes>
                <total-fare>4887.40</total-fare>
            </pricing-summary> -
            <flights> -
                <flight> -
                    <segments> -
                        <segment>
                            <index>1</index>
                            <departure-airport>BOM</departure-airport>
                            <arrival-airport>CCU</arrival-airport>
                            <departure-date-time>2008-11-01T09:20:00</departure-date-time>
                            <arrival-date-time>2008-11-01T12:00:00</arrival-date-time>
                            <airline>6E</airline>
                            <flight-number>321</flight-number>
                            <operating-airline>6E</operating-airline>
                            <stops>0</stops>
                            <equipment>320</equipment>
                            <duration>9600</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>H0BOMCCU</fare-basis-code>
                            <fare-key>ZG9tZXN0aWMtNkUtUi1IMEJPTUNDVS0zNDMzNTc3MjUw</fare-key> -
                            <pricing-elements> -
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>CLEARTRIP-SVC</code>
                                    <amount>12.40</amount>
                                </pricing-element> -
                                <pricing-element>
                                    <category>BF</category>
                                    <amount>2000.00</amount>
                                </pricing-element> -
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>PSF</code>
                                    <amount>225.00</amount>
                                </pricing-element> -
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>YQ</code>
                                    <amount>2350.00</amount>
                                </pricing-element> -
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>YR</code>
                                    <amount>150.00</amount>
                                </pricing-element> -
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>TF</code>
                                    <amount>100.00</amount>
                                </pricing-element> -
                                <pricing-element>
                                    <category>TAX</category>
                                    <code>AIRLINE-MSC</code>
                                    <amount>50.00</amount>
                                </pricing-element> -
                                <pricing-element>
                                    <category>MKP</category>
                                    <amount>0.00</amount>
                                </pricing-element> -
                                <pricing-element>
                                    <category>DIS</category>
                                    <amount>0.00</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>R</booking-class>
                            <cabin-type>E</cabin-type>
                            <ticket-type>E</ticket-type>
                        </booking-info>
                    </booking-info-list>
                </pax-pricing-info>
            </pax-pricing-info-list>
        </solution> -
      </onward-solutions>
</air-search-result>

我是php的新手。如果还有更好的方法可以做到这一点,请建议。

1 个答案:

答案 0 :(得分:0)

经过进一步研究,我终于得到了预期的结果: - 显然,只需要进一步采取循环..

这是代码:

    <?php 

$contents= simplexml_load_file('xml/cleartrip.xml');
$onward_flights = $contents->{"onward-solutions"};

    foreach ($onward_flights->solution as $onwardflights) 
        {

                $flightno= $onwardflights->flights->flight->segments->segment->airline.'-'.$onwardflights->solution->flights->flight->segments->segment->{"flight-number"};
                $airline= $flightno;
                $depdate= substr($onwardflights->flights->flight->segments->segment->{"departure-date-time"},0,10);
                $deptime= substr($onwardflights->flights->flight->segments->segment->{"departure-date-time"},11,8);
                $arrdate= substr($onwardflights->flights->flight->segments->segment->{"arrival-date-time"},0,10);
                $arrtime= substr($onwardflights->flights->flight->segments->segment->{"arrival-date-time"},11,8);
                $duration= $onwardflights->flight->segments->segment->duration;
                $stops= $onwardflights->flights->flight->segments->segment->stops;
                $origin= substr(filter_input(INPUT_POST, "origin"), 0, 3);
                $destination= substr(filter_input(INPUT_POST, "destination"), 0, 3);
                $fare= $onwardflights->{"pricing-summary"}->{"total-fare"};
                echo $key." ".$flightno." ".$depdate." ".$arrdate." ".$fare." ".$airline." ".$deptime." ".$arrtime." ".$duration." ".$stops." ".$origin." ".$destination."<br/>";

    }





?>