2

I'm using Codeigniter 3 and I tried to take value of xml. I didn't handle..

I tried to take value with this code.

        $dom = new DOMDocument();
        $dom->loadlXML($xml);
        $img = $dom->getElementsByTagName('sonucKodu')->item(0);
        echo $img->attributes->getNamedItem("value")->value;

My xml file.

<?xml version="1.0" encoding="UTF-8"?>
<SYSMessage xmlns:ext="http://nxxx/Cd/Extensions/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <messageType code="1" value="xxx" codeSystemGuid="0a9ba485-e7e0-4abb-9c86-0a14fd364bb8" version="1" />
   <documentGenerationTime value="201512041717" />
   <author>
      <healthcareProvider code="0" value="SYS" codeSystemGuid="c9dbe1cb-57cb-48fb-bdd3-d622e0e304c6" version="1" />
   </author>
   <recordData>
      <KayitCevabi>
         <sonucKodu value="E2003" />
         <sonucMesaji value="TakipNo geçerli değil" />
      </KayitCevabi>
   </recordData>
</SYSMessage>

I just need value of attribute sonucKodu. It must be E2003.

Thanks for your helping.

chris85
  • 23,846
  • 7
  • 34
  • 51

2 Answers2

1

I'd use SimpleXMLElement rather than Domdocument.

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<SYSMessage xmlns:ext="http://nxxx/Cd/Extensions/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <messageType code="1" value="xxx" codeSystemGuid="0a9ba485-e7e0-4abb-9c86-0a14fd364bb8" version="1" />
   <documentGenerationTime value="201512041717" />
   <author>
      <healthcareProvider code="0" value="SYS" codeSystemGuid="c9dbe1cb-57cb-48fb-bdd3-d622e0e304c6" version="1" />
   </author>
   <recordData>
      <KayitCevabi>
         <sonucKodu value="E2003" />
         <sonucMesaji value="TakipNo geçerli değil" />
      </KayitCevabi>
   </recordData>
</SYSMessage>';
$s_xml = new SimpleXMLElement($xml);
//print_r($s_xml);
echo $s_xml->recordData->KayitCevabi->sonucKodu['value'];

Output:

E2003

Demo: https://eval.in/480525

chris85
  • 23,846
  • 7
  • 34
  • 51
0
<?php
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>";

$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object");
print_r($xml);
?>
AldoZumaran
  • 547
  • 5
  • 23