2012-01-06 105 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

相关问题