【问题标题】:Extract table data from Wikipedia and convert into an XML document从 Wikipedia 中提取表格数据并转换为 XML 文档
【发布时间】:2012-02-04 14:17:46
【问题描述】:

页面:http://en.wikipedia.org/wiki/ISO_4217#Active_codes

是否可以提取每个:

  • 货币代码
  • 货币名称
  • 货币位置

如果可能的话,像这样保存到 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

谢谢。

【问题讨论】:

    标签: php xml


    【解决方案1】:

    该表以 wiki 格式创建并因此可用: http://en.wikipedia.org/w/index.php?title=ISO_4217&action=edit&section=4

    您可以编写一个脚本来将 wiki 格式解析为一个数组,并从中构建一个 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

    【讨论】:

      猜你喜欢
      • 2012-10-21
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多