Search code examples
phpxml-parsingxml-namespacesixmldomdocument

reading namespace xml data using php


I have xml data as

        <ns:PartyIDs>
          <ns:ID schemeName="PartyTypeNumber">009</ns:ID>
          <ns:ID schemeName="PartyNumber">00038</ns:ID>
          <ns:ID schemeName="PartySubNumber">00038</ns:ID>
        </ns:PartyIDs>

i need to read this and print the attributes and value. Using PHP.

right now i am trying usin DOMDocument

 $soap_do = curl_init(); 
curl_setopt($soap_do, CURLOPT_URL,            $url ); 
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_TIMEOUT,        10); 
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($soap_do, CURLOPT_POST,           true ); 
curl_setopt($soap_do, CURLOPT_POSTFIELDS,    $domObject->saveXML()); 
curl_setopt($soap_do, CURLOPT_HTTPHEADER,     array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($domObject->saveXML()) )); 

 $response = curl_exec($soap_do);  
    $xml = new SimpleXMLElement($response);   //file from response
     $fileName="Response".date("Ymd_H_i_s");
      $xml->saveXML($fileName);

 $doc = new DOMDocument();
 $doc->load($fileName);//loading response file  

Solution

  • Use $doc->getElementsByTagNameNS() instead.

    See the manual for details: http://php.net/DOMDocument.getElementsByTagNameNS

    You'll need to provide the $namespaceUri parameter which should have been specified in the root element of the XML file as xmlns:ns="http://...".