1. 程式人生 > >php解析帶有命名空間的xml

php解析帶有命名空間的xml

highlight div -i light cti tab pan reat log

xml如果帶有命名空間我們將如何解析,例如:

<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>/neusoft.com">01:strFlag><ns1< span="">:strHpCode xsi:nil="true" xmlns="http://neusoft.com"/><ns1:strReason xmlns="
http://neusoft.com">,產品編碼為空,售達方編碼為空</ns1:strReason>1:OutVo>
:out>:CreateBillResponse>
解析的方法:
1 function parseNamespaceXml($xmlstr)
2 {
3
4 $xmlstr = preg_replace(‘/\sxmlns="(.*?)"/‘, ‘ _xmlns="${1}"‘, $xmlstr);
5 $xmlstr = preg_replace(‘/<(\/)?(\w+):(\w+)/‘, ‘<${1}${2}_${3}‘, $xmlstr);
6
$xmlstr = preg_replace(‘/(\w+):(\w+)="(.*?)"/‘, ‘${1}_${2}="${3}"‘, $xmlstr);
7 $xmlobj = simplexml_load_string($xmlstr);
8 return json_decode(json_encode($xmlobj), true);
9 }

php解析帶有命名空間的xml