【问题标题】:Why attributes is not parsing with simplexml_load_string为什么不使用 simplexml_load_string 解析属性
【发布时间】:2019-04-16 09:03:58
【问题描述】:

我正在尝试将 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);

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

谁能指导我如何成功地将 xml 转换为数组? 谢谢。

【问题讨论】:

    标签: php simplexml


    【解决方案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"
    
    ...
    

    【讨论】:

    • 谢谢,这是可行的,但是使用它比 simplexml_load_string 有什么优势吗?有没有其他方法可以在不改变数组结构的情况下实现这一目标?因为我的问题只有在xml有属性和值的情况下才存在,如果xml标签只有属性或值,则解析成功
    【解决方案2】:

    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 的属性

    【讨论】:

    • 是的,var_dump 和 print_r 不显示嵌套对象,但是当我使用 json_encode 和 json_decode 进行转换时,结果数组中缺少该对象
    猜你喜欢
    • 2019-12-05
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多