1

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

1
  • As you already have an object, you don't need to create an array out of it, do you? Commented Dec 22, 2014 at 15:37

2 Answers 2

1

This is how I did it based on Ghost answer.

$reports = array ();
foreach($study->reports->file as $rep)
{
  $temp = array();
  $temp['repName'] = (String)$rep;

  // Getting cat and date attributes of the report
  foreach($rep->attributes() as $key => $rep)
  {
    $temp[$key] = (string) $rep;
  }

  $reports[] = $temp;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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
                )

        )

)

2 Comments

I am still not sure why you cant see it while you are in the debug variables or when you print_r()/var_dump(). Any idea ?
@Sinaaria here is a very eloquent answer regarding that concern stackoverflow.com/questions/12432739/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.