xml若是帶有命名空間咱們將如何解析,例如:json
<ns1:CreateBillResponse xmlns:ns1="http://neusoft.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><ns1:out><ns1:OutVo><ns1:strB2BCode xmlns="http://neusoft.com">AM1432690905685938443</ns1:strB2BCode><ns1:strFlag xmlns="http://neusoft.com">0</ns1:strFlag><ns1:strHpCode xsi:nil="true" xmlns="http://neusoft.com"/><ns1:strReason xmlns=" http://neusoft.com">,產品編碼爲空,售達方編碼爲空</ns1:strReason></ns1:OutVo></ns1:out></ns1:CreateBillResponse>
解析的方法:編碼
function parseNamespaceXml($xmlstr) { $xmlstr = preg_replace('/\sxmlns="(.*?)"/', ' _xmlns="${1}"', $xmlstr); $xmlstr = preg_replace('/<(\/)?(\w+):(\w+)/', '<${1}${2}_${3}', $xmlstr); $xmlstr = preg_replace('/(\w+):(\w+)="(.*?)"/', '${1}_${2}="${3}"', $xmlstr); $xmlobj = simplexml_load_string($xmlstr); return json_decode(json_encode($xmlobj), true); }