Search code examples
phpxmlrestxml-parsingsimplexml

how to parse xml which has ns2 element


$xml_data = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<ns2:orders xmlns:ns2="http://skt.tmall.business.openapi.spring.service.client.domain">
<ns2:order>
<ordNo>201303285538019</ordNo>
<ordPrdSeq>1</ordPrdSeq>
<addPrdNo>0</addPrdNo>
<addPrdYn>N</addPrdYn>
<ordEmail>[email protected]</ordEmail>
<clearanceEmail>[email protected]</clearanceEmail >
<dlvCstType>03</dlvCstType>
<dlvNo>140065055</dlvNo>
<lstDlvCst>0</lstDlvCst>
<lstSellerDscPrc>0</lstSellerDscPrc>
<lstTmallDscPrc>0</lstTmallDscPrc>
<memID>[email protected]</memID>
<memNo>25879997</memNo>
<ordAmt>0</ordAmt>
<ordCnQty>0</ordCnQty>
<ordDt>04-03-2014 10:40:32</ordDt>
<ordNm>Kil Jun Jeong</ordNm>
<ordOptWonStl>300000</ordOptWonStl>
<ordPayAmt>0</ordPayAmt>
<ordPrtblTel>010-2988-6401</ordPrtblTel>
<ordPrdStat>901</ordPrdStat>
<ordQty>0</ordQty>
<ordTlphnNo>070-7400-3141</ordTlphnNo>
<prdNm>test product name</prdNm>
<prdNo>233607759</prdNo>
<prdStckNo>1532843428</prdStckNo>
<rcvrBaseAddr>50929, Kuala Lumpur, Wilayah Persekutuan Kuala Lumpur
</rcvrBaseAddr>
<rcvrDtlsAddr>jl setia budi </rcvrDtlsAddr>
<rcvrMailNo>156010</rcvrMailNo>
<rcvrNm>Kil Jun Jeong</rcvrNm>
<rcvrTlphn>070-7400-3141</rcvrTlphn>
<selPrc>100000</selPrc>
<sellerDscPrc>0</sellerDscPrc>
<tmallDscPrc>0</tmallDscPrc>
</ns2:order>
</ns2:orders>            
';

This is my xml and I want to parse whole xml, I tried with the below code

$xml = simplexml_load_string($xml_data);
print_r($xml);

but its return empty object.

SimpleXMLElement Object ( )

I tried with children xml parsing but same result. I followed this link too but no success


Solution

  • You can use this :

    $xml_data = str_replace("ns2:","",$xml_data);
    $xml = simplexml_load_string($xml_data);
    print_r($xml);