Skip to content
Advertisement

¿How to get the data of XML with php?

I have the next problem, in my code I get a SOAP response in XML format so I want to loop it to get the info that the XML contain, the xml looks like this:

<?php
$xml_response = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<REPORTES>
    DEMO
    <REPORTE TIPO="BASIC">
        <INVESTIGADO>SOMETHING</INVESTIGADO>
        <FECHA>SOMETHING</FECHA>
        <HORA>SOMETHING</HORA>
        <BASE>SOMETHING

            <PERIODO>SOMETHING</PERIODO>
            <JUICIO>SOMETHING</JUICIO>
            <GRADO>SOMETHING</GRADO>
        </BASE>
        <GRADO_MAXIMO>SOMETHING</GRADO_MAXIMO>
        <CASOS>
            <DEFINICION>SOMETHING</DEFINICION>
            <TIEMPO>SOMETHING</TIEMPO>
        </CASOS>
        <PDF_REPORT>SOMETHING</PDF_REPORT>
    </REPORTE>
    <REPORTE TIPO="DETAIL">
        <BASE>SOMETHING
            <INFORMACION>
                <JUZGADO>SOMETHING</JUZGADO>
                <JUZ_NUM>SOMETHING</JUZ_NUM>
                <SECRETARIA>SOMETHING</SECRETARIA>
                <FECHA_ACU>SOMETHING</FECHA_ACU>
                <NUM_BOL>SOMETHING</NUM_BOL>
                <FECHA_PUB>SOMETHING</FECHA_PUB>
                <RUBRO>SOMETHING</RUBRO>
            </INFORMACION>
        </BASE>
    </REPORTE>
</REPORTES>
EOF;
?>

I’m trying to get the value of the attribute “TIPO” because the child have a different structure, the problem is when the code prints the value only print the first

$xml = simplexml_load_string($xml_response);
foreach ($xml->children()->attributes() as $key => $val) {
    echo "Key: " . $key . " value: " . $val . "<br>";
    var_dump($xml);
}

The output of the code

object(SimpleXMLElement)#1 (1) { ["REPORTE"]=> array(2) { [0]=> object(SimpleXMLElement)#7 (8)
 { ["@attributes"]=> array(1) { ["TIPO"]=> string(5) "BASIC" } ["INVESTIGADO"]=> string(9) 

"SOMETHING" ["FECHA"]=> string(9) "SOMETHING" ["HORA"]=> string(9) "SOMETHING" ["BASE"]=> string(25) 

"SOMETHING " ["GRADO_MAXIMO"]=> string(9) "SOMETHING" ["CASOS"]=> object(SimpleXMLElement)#9 (2) { 

["DEFINICION"]=> string(9) "SOMETHING" ["TIEMPO"]=> string(9) "SOMETHING" } ["PDF_REPORT"]=> 

string(9) "SOMETHING" } [1]=> object(SimpleXMLElement)#8 (2) 

{ ["@attributes"]=> array(1)
 { ["TIPO"]=> string(6) "DETAIL" } ["BASE"]=> string(16) "SOMETHING " } } }
Key: TIPO value: BASIC

I made the var_dumb to be sure that the all childs are present. Finally the question is… How can I make that the foreach get the attributes of child “REPORTES”?

Advertisement

Answer

The documentation doesn’t really specify this, but you need to loop on the children method, or assign that value to a var before starting the loop. Basically do not chain your attributes method. Then it will work as expected.

<?php

$xml = simplexml_load_string($xml_response);
foreach ($xml->children() as $child) {
  foreach($child->attributes() as $key => $val) {
      echo "Key: " . $key . " value: " . $val . "<br>";
    }  
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement