Monday, January 17, 2011

Php accessing XML with NameSpace Simple Example

<?php
$xmlString="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
     <ns0:Tango xmlns:ns0=\"http://some.com/schemas/Tango/1.0.0\">
    <ns0:Charlie>
        <ns0:Papi>
            <ns0:Carbon> SCANNER.TIF302.42.236</ns0:Carbon>
        <ns0:Creator>1545421</ns0:Creator>
         <ns0:Jaco>130803</ns0:Jaco>
       </ns0:Papi>
  </ns0:Charlie></ns0:Tango>";
$xml = simplexml_load_string($xmlString);
$dc = $xml->children('ns0',true)->children('ns0',true)->children('ns0',true);

//I wanted to access the value in Creator Node
echo $dc;

Php and Simple XML with namespace

<?php
$xmlString=<<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
     <Processrere>
    <ProcessDispositionDocumentResponses>
        <ProcessDispositionDocumentResponse>
            <ns0:Rsere> SCANNER.TIF302.42.236</ns0:Rsere>
        <ns0:Doser>1545421</ns0:Doser>
         <ns0:Caseser>130803</ns0:Caseser>
       </ns0:ProcessDispositionDocumentResponse>
  </ns0:ProcessDispositionDocumentResponses></ns0:Processrere>
XML;

$modifiedData = str_replace("ns0:", "", $xmlString);
$xml = simplexml_load_string($xmlString1);
$xml = simplexml_load_string($modifiedData);

//echo $xml->getName() . "<br />";

foreach($xml->children()->children()->children() as $child)
  {
 if ($child->getName() ==="CaseNumber"){
     $caseNumber=$child;
 }
  }
  echo $caseNumber;