我正在尝试将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转换为数组吗? 谢谢。
答案 0 :(得分:1)
您可以使用@Embedded(value = "house_price_")
Amount approvalValue;
创建一个新的XML解析器。使用xml_parser_create()
将XML数据解析为数组结构。
xml_parse_into_struct()
//输出:
<?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>';
答案 1 :(得分: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的属性