【发布时间】:2016-06-10 12:36:10
【问题描述】:
我需要将 XML 转换为数组,但它没有转换
这是我的代码
<?php
$response='<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Search xmlns="http:url">
<Request>
<aaa>string</aaa>
<bbb>string</bbb>
<ccc>srting</ccc>
<SourceName>string</SourceName>
</Request>
</Search>
</soap:Body>
</soap:Envelope>';
function xml2Array($xmlstring)
{
$xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
return json_decode($json,TRUE);
}
$arr = xml2Array($response);
print_r($arr);
但是如果我删除了
<soap:Body>
从 XML 它工作正常,有什么问题如何解决它
【问题讨论】:
-
不幸的是,当涉及命名空间时,SimpleXML 不再简单。您可能根本无法使用
json_encode($xml)技巧。您真的需要任何 XML 定义的通用解决方案吗? -
@AlvaroGonzalez 有没有替代品?