[英]How to convert xml to array in php?
My code is like this : 我的代码是这样的:
$xmlstring = '<Response>
<Auth>
<UserID>test</UserID>
<UserIP>123</UserIP>
<XmlVersion>Testing Version</XmlVersion>
</Auth>
<SearchAvailRequest>
<CountryCd>ID</CountryCd>
<CityCd>JOG</CityCd>
<CheckIn>2016-01-08</CheckIn>
<CheckOut>2016-01-09</CheckOut>
</SearchAvailRequest>
<SearchAvailResponse>
<Hotel>
<HotelNo>1</HotelNo>
<HotelCode>321</HotelCode>
<HotelName>Test 1 Hotel</HotelName>
<RmGrade>Deluxe</RmGrade>
<RmGradeCode>DLX</RmGradeCode>
</Hotel>
<Hotel>
<HotelNo>2</HotelNo>
<HotelCode>212</HotelCode>
<HotelName>Test 2 & 1 Hotel</HotelName>
<RmGrade>Deluxe</RmGrade>
<RmGradeCode>DLX</RmGradeCode>
</Hotel>
</SearchAvailResponse>
</Response>';
$xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
echo '<pre>';print_r($array);echo '</pre>';
?>
I want to convert the XML to PHP array. 我想将XML转换为PHP数组。 But I found an error like this : 但是我发现了这样的错误:
Warning: simplexml_load_string(): Entity: line 24: parser error : xmlParseEntityRef: no name in C:\xampp\htdocs\ngetest\xml.php on line 268
Warning: simplexml_load_string(): <HotelName>Test 2 & 1 Hotel</HotelName> in C:\xampp\htdocs\ngetest\xml.php on line 268
Warning: simplexml_load_string(): ^ in C:\xampp\htdocs\ngetest\xml.php on line 268
This seems to happen because there is exist and symbol(Test 2 & 1 Hotel). 这似乎是由于存在和符号(Test 2&1 Hotel)而发生的。 I have not found the right solution to solve this problem 我没有找到解决此问题的正确方法
Any suggestions on how I can solve this problem? 关于如何解决这个问题有什么建议吗?
Thank you very much 非常感谢你
Try this: 尝试这个:
$xmlparser = xml_parser_create();
xml_parse_into_struct($xmlparser,$xmlstring,$values);
xml_parser_free($xmlparser);
print_r($values);
Hope this helps you. 希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.