2012-10-05 107 views
0

我想把一個XML對象放入一個數組,但我得到一個emty數組,我找不到原因。dom保存元素到字符串

<?php 

$string = '<?xml version="1.0" encoding="UTF-8"?> 
      <confgs> 
      # System Settings # 
      ############################################################ 
      ## Login Account Settings 

      ADMIN_ID="<confg rang="0" soort="veld" name="config1" />" 
      ADMIN_ID="<confg rang="1" soort="veld" name="config2" />" 
      ADMIN_PASS="PKXUT1xxRoutIT" 
      USER_ID="<confg rang="2" soort="veld" name="config3" />" 
      USER_PASS="<confg rang="3" soort="optie" opties="opt1~opt2~opt3" name="config4" />" 
      ## Time Adjust Settings 
      TIME_ZONE="60" 
      DST_ENABLE="Y" 
      MAC_ADRES="<confg rang="4" soort="veld" name="config5" />" 
      DST_START_MONTH="3" 
      DST_START_ORDINAL_DAY="<stconfg rang="1" soort="veld" name="stanconf1" />" 
      DST_START_ORDINAL_DAY="<confg rang="8" soort="optie" opties="deze~die~endit~optie~extra" name="kijkhetwerkt" />" 
      DST_START_ORDINAL_DAY="5" 
      DST_START_ORDINAL_DAY="<stconfg rang="2" soort="veld" name="stanconf2" />" 
      DST_START_ORDINAL_DAY="<confg rang="5" soort="veld" name="config6" />" 
      DST_START_ORDINAL_DAY="<confg rang="6" soort="optie" opties="dit~dat~bibobla" name="slelect" />" 
      IETS_CONF_ORDINAL_CFG="<confg rang="7" soort="veld" name="ditveld" />" 
      IETS_CONF_ORDINAL_CFG="<confg rang="9" soort="veld" name="hallo" />" 
      </confgs> 
      '; 

    error_reporting(E_ALL^E_NOTICE); 
    $dom = new DOMDocument(); 
    $dom->preserveWhiteSpace = false; 
    $dom->loadXML($string); 
    $confg = $dom->getElementsByTagName('confg'); 

    $form = array(); 
    $a = 0; 

    foreach($confg as $configuratie){ 
     $a++; 
     $form[$a] = $dom->saveXML($configuratie); 
    } 

print_r($form); 
    ?> 

這是我得到:

陣列([1] => [2] => [3] => [4] => [5] => [6] => [有人告訴我,我的代碼循環遍歷所有10個元素,所以它必須在那裏,我該如何抓住它?

回答

1

這是因爲<confg ... />現在是HTML元素,無法在瀏覽器中顯示,因爲它們只是HTML標記。

如果您查看源代碼,您將看到它們。

+0

我現在覺得這麼愚蠢!你是如此正確,他們在那裏我一直沒有看到他們,我現在就做。而且我已經在想,爲什麼這不是工作,但它一直工作。 – botenvouwer

+0

:)它經常發生,第二雙眼睛總是最好的。 –

+0

由於某些原因,我無法在str_replace函數內使用數組的值。爲什麼是這樣? – botenvouwer

相關問題