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 ) |
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); |