2012-01-06 67 views
2

頁:http://en.wikipedia.org/wiki/ISO_4217#Active_codes提取表中的數據並將其轉換爲XML文檔

是否可以提取每:

  • 貨幣代碼
  • 貨幣標題
  • 貨幣位置

並且如果可能,保存到XML文檔中,如下所示:

<currency> 
    <AED> 
     <curr>United Arab Emirates dirham</curr> 
     <loc>United Arab Emirates</loc> 
    </AED> 
</currency> 
<currency> 
    <AFN> 
     <curr>Afghan afghani</curr> 
     <loc>Afghanistan</loc> 
    </AFN> 
</currency> 

我不知道如果這有助於但是我發現,你可以在wiki頁面轉換成XML幾分結構:

http://en.wikipedia.org/wiki/Special:Export/ISO_4217#Active_codes

感謝。

回答

2

表創建完畢,因而可用,對維基格式: http://en.wikipedia.org/w/index.php?title=ISO_4217&action=edit&section=4

你可以寫一個腳本來分析維基格式到一個數組中,並建立從一個XML。嘗試使用換行符分割字符串(例如,使用explode),然後將每行分割爲||,這將分隔表格列。

事情是這樣的:

$currencyList = array(); 
$source = "<insert wikipedia table code here>"; 

$rows = explode("\n", $source); // split the table in rows 

foreach($rows as $row) { 

    if(strlen(trim($row)) < 0) { continue; } // ignore empty rows 
    if(trim($row) == "|-") { continue; } // ignore table line separators 

    $row = substr($row, 2); // remove the "| " from the beginning of each row 

    $cols = explode("||", $row); // split the row in columns 

    $currency = array(// clean data and store in associative array 
     'code' => trim($cols[0]), 
     'number' => trim($cols[1]), 
     'digits_after_decimal' => trim($cols[2]), 
     'name' => trim($cols[3]) 
    ); 

    array_push($currencyList, $currency); // add the read currency to the list 

} 

var_dump($currencyList); // $currencyList now has a list of associative arrays with your data. 

要構建XML,你可以嘗試PHP的SimpleXML

+0

這件作品像魅力!謝謝。 – Nirmal 2012-02-27 05:55:19

相關問題