Search code examples
phpxmlsimplexml

Getting XML attribute from the inner node using PHP SimpleXML is failing


Chaps, I am trying to get attributes of the 'file' node in the following XML with SimpleXml but every time is gives me back null. I have successfully got the attributes for the study node but fail to get the 'file' atts .

here is the xml :

<?xml version="1.0" encoding="UTF-8"?>
<studies>
  <study uid="1.3.12.2" acc="181">
    <date>20051218</date>
    <time>2156</time>
    <ref>CG</ref>
    <desc>Abdomen</desc>
    <id></id>
    <path>S00001</path>
    <modality>CR</modality>
    <reports>
  <file cat="UNK" date="20141124">Card_Cloud.txt</file>
</reports>
</study>

and here is my code :

$studyXML = new SimpleXMLElement($studyXML);
$studyXML_array = array();
foreach ($studyXML ->study as $study)
{

// Getting uid and accession from XML attributes
$uid = (!empty($study)) ? (String)$study->attributes()->uid : '';
$acc = (!empty($study)) ? (String)$study->attributes()->acc : '';



// Getting the reports and putting them in an array
$reports = array ();
foreach($study->reports as $rep)
{
  $cat = (String)$rep->attributes()->cat;
  $reports[] = (String)$rep->file;

}

// Constructing the xml as an array
$studyXML_array[] = array
                    (
                      'uid'      => $uid,
                      'acc'      => $acc,
                      'date'     => (String)$study->date,                          
                      'reports'  => $reports
                    );

}

I can get "uid" and "acc" but I can't get "cat" and "date" inside file node. When I look at the array of the xml in my debug variables I can see uid and acc attributes but no sign of cat and date attributes.

I Would really appreciate any help.

I prefer to stick to SimpleXML as all my code is using that so far.

Cheers


Solution

  • If you're trying to use print_r()/var_dump() to check, then it will do no justice. But it is there, try to traverse to it along with using ->attributes() inside the foreach as well:

    $studyXML = new SimpleXMLElement($studyXML);
    $studyXML_array = array();
    foreach ($studyXML ->study as $study)
    {
    
        // Getting uid and accession from XML attributes
        $uid = (!empty($study)) ? (String)$study->attributes()->uid : '';
        $acc = (!empty($study)) ? (String)$study->attributes()->acc : '';
    
        // Getting the reports and putting them in an array
        $reports = array();
        // loop each attribute
        foreach($study->reports->file->attributes() as $key => $rep)
        {
            $reports[$key] = (string) $rep;
        }
    
        // Constructing the xml as an array
        $studyXML_array[] = array(
            'uid'      => $uid,
            'acc'      => $acc,
            'date'     => (String) $study->date,
            'reports'  => $reports
            );
    }
    
    echo '<pre>';
    print_r($studyXML_array);
    

    Sample Output:

    Array
    (
        [0] => Array
            (
                [uid] => 1.3.12.2
                [acc] => 181
                [date] => 20051218
                [reports] => Array
                    (
                        [cat] => UNK
                        [date] => 20141124
                    )
    
            )
    
    )