2014-12-04 114 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; 
}