为什么属性不使用simplexml_load_string进行解析

问题描述 投票:-1回答:2

我试图解析xml到数组这里是我的样本xml

<?xml version="1.0" encoding="utf-8"?>
<Service_GetCancelPolicy>
    <GetCancelPolicy_Response>
        <ResNo>ELOKMA190410940</ResNo>
        <HBooking>HBMA1904000294</HBooking>
        <HotelId>WSASIDDPS001302</HotelId>
        <HotelName>Inna Bali Heritage Hotel</HotelName>
        <Policies>
            <Policy FromDate="2019-03-01" ToDate="2019-08-31">
                <RoomCatgCode Name="SULTAN" BFType="NONE">WSMA02045946</RoomCatgCode>
                <ExCancelDays>7</ExCancelDays>
                <ChargeType>Percent</ChargeType>
                <ChargeRate>80.00</ChargeRate>
                <Description></Description>
            </Policy>
        </Policies>
    </GetCancelPolicy_Response>
</Service_GetCancelPolicy>

这是我得到的数组结果

Array
(
    [GetCancelPolicy_Response] => Array
        (
            [ResNo] => ELOKMA190410940
            [HBooking] => HBMA1904000294
            [HotelId] => WSASIDDPS001302
            [HotelName] => Inna Bali Heritage Hotel
            [Policies] => Array
                (
                    [Policy] => Array
                        (
                            [@attributes] => Array
                                (
                                    [FromDate] => 2019-03-01
                                    [ToDate] => 2019-08-31
                                )

                            [RoomCatgCode] => WSMA02045946
                            [ExCancelDays] => 7
                            [ChargeType] => Percent
                            [ChargeRate] => 80.00
                            [Description] => Array
                                (
                                )

                        )

                )

        )

)

这就是我如何做到的

$xml = '<?xml version="1.0" encoding="utf-8"?><Service_GetCancelPolicy><GetCancelPolicy_Response><ResNo>ELOKMA190410940</ResNo><HBooking>HBMA1904000294</HBooking><HotelId>WSASIDDPS001302</HotelId><HotelName>Inna Bali Heritage Hotel</HotelName><Policies><Policy FromDate="2019-03-01" ToDate="2019-08-31"><RoomCatgCode Name="SULTAN" BFType="NONE">WSMA02045946</RoomCatgCode><ExCancelDays>7</ExCancelDays><ChargeType>Percent</ChargeType><ChargeRate>80.00</ChargeRate><Description></Description></Policy></Policies></GetCancelPolicy_Response></Service_GetCancelPolicy>';

$xml = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

print_r($array);

我的问题是为什么RoomCatgCode属性包括Name和BFType没有显示在数组上?如果不存在RoomCatgCode值(WSMA02045946),则RoomCatgCode属性位于结果数组上,如Policy FromDate和ToDate属性

任何人都可以指导我如何成功地将该xml转换为数组?谢谢。

php simplexml-load-string
2个回答
1
投票

您可以使用xml_parser_create()创建新的XML解析器。使用xml_parse_into_struct()将XML数据解析为数组结构。

<?php
$xml = '<?xml version="1.0" encoding="utf-8"?><Service_GetCancelPolicy><GetCancelPolicy_Response><ResNo>ELOKMA190410940</ResNo><HBooking>HBMA1904000294</HBooking><HotelId>WSASIDDPS001302</HotelId><HotelName>Inna Bali Heritage Hotel</HotelName><Policies><Policy FromDate="2019-03-01" ToDate="2019-08-31"><RoomCatgCode Name="SULTAN" BFType="NONE">WSMA02045946</RoomCatgCode><ExCancelDays>7</ExCancelDays><ChargeType>Percent</ChargeType><ChargeRate>80.00</ChargeRate><Description></Description></Policy></Policies></GetCancelPolicy_Response></Service_GetCancelPolicy>';

$handle = xml_parser_create();
/* Parse XML data into an array structure */
xml_parse_into_struct($handle, $xml, $values);
xml_parser_free($handle);

echo '<pre>';
var_dump($values);
echo '</pre>';

//输出:

...

[8]=>
  array(5) {
    ["tag"]=>
    string(12) "ROOMCATGCODE"
    ["type"]=>
    string(8) "complete"
    ["level"]=>
    int(5)
    ["attributes"]=>
    array(2) {
      ["NAME"]=>
      string(6) "SULTAN"
      ["BFTYPE"]=>
      string(4) "NONE"
    }
    ["value"]=>
    string(12) "WSMA02045946"

...

0
投票

var_dump和print_r不显示嵌套对象。试试这个:

$xml = '<?xml version="1.0" encoding="utf-8"?><Service_GetCancelPolicy><GetCanc$

$result = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

foreach ($result[0]->children()[0] as $elem) {
   foreach($elem->children()[0] as $x) {
        var_dump($x);
   }
}

你会看到RoomCatgCode的属性

© www.soinside.com 2019 - 2024. All rights reserved.