2014-12-04 119 views
1

Chaps, 我试图用SimpleXml获取以下XML中'文件'节点的属性,但每次都会让我回到空。我已成功获取学习节点的属性,但未能获取'文件'atts。使用PHP SimpleXML从内部节点获取XML属性失败

这里是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> 

这里是我的代码:

$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 
        ); 

} 

我可以得到 “UID” 和 “ACC”,但我不能让“猫“和”日期“在文件节点内。 当我在调试变量中查看xml的数组时,我可以看到uid和acc属性,但没有cat和date属性的符号。

我会很感激任何帮助。

我更愿意坚持使用SimpleXML,因为我所有的代码都使用到目前为止。

干杯

+0

因为您已经有了一个对象,您不需要创建一个数组,您呢? – hakre 2014-12-22 15:37:55

回答

0

如果你正在尝试使用print_r()/var_dump()检查,然后它会做不正义。但它的存在,试图穿越它使用->attributes()在foreach内部以及沿:

$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 
       ) 

     ) 

) 
+0

谢谢我的朋友。你的答案有窍门。 – 2014-12-05 01:09:01

+0

@Sinaaria很高兴这有帮助 – Ghost 2014-12-05 01:11:03

+0

我仍然不确定为什么你不能在调试变量或print_r()/ var_dump()时看到它。任何想法 ? – 2014-12-05 06:18:57

1

这是我如何做它的基础上鬼答案。

$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; 
}