【发布时间】:2010-05-12 03:10:05
【问题描述】:
我想使用 XML::XPath 解析器从 Worldbank 站点的 XML DB 文件中提取数据。问题是我在输出中没有看到任何结果。我一定在代码中遗漏了一些东西。理想情况下,我只想从每个国家 XML DB 中提取死亡率统计数据(年份和价值)。我将其用作输入的一部分:
http://data.worldbank.org/sites/default/files/countries/en/afghanistan_en.xml
use strict;
use LWP 5.64;
use HTML::ContentExtractor;
use XML::XPath;
my $agent1 = LWP::UserAgent->new;
my $extractor = HTML::ContentExtractor->new();
#Retrieve main Worldbank country site
my $mainlink = "http://data.worldbank.org/country/";
my $page = $agent1->get("$mainlink");
my $fulltext = $page->decoded_content();
#Match to just all available countries in Worldbank
my $country = "";
my @countryList;
if (@countryList = $fulltext =~ m/(http:\/\/data\.worldbank\.org\/country\/.*?")/gi){
foreach $country(@countryList){
#Remove " at the end of link
$country=~s/\"//gi;
print "\n" . $country;
#Retrieve each country profile's XML DB file
my $page = $agent1->get("$country");
my $fulltext = $page->decoded_content();
my $XML_DB = "";
my @countryXMLDBList;
if (@countryXMLDBList = $fulltext =~ m/(http:\/\/data\.worldbank\.org\/sites\/default\/files\/countries\/en\/.*?\.xml)/gi){
foreach $XML_DB(@countryXMLDBList){
my $page = $agent1->get("$XML_DB");
my $fulltext = $page->decoded_content();
#print $fulltext;
#Use XML XPath parser to find elements related to death rate
my $xp = XML::XPath->new($fulltext); #my $xp = XML::XPath->new("afghanistan_en.xml");
my $nodeSet = $xp->find("//*");
if (!$nodeSet->isa('XML::XPath::NodeSet') || $nodeSet->size() == 0) {
#No match found
print "\nMatch not found!";
exit;
} else {
foreach my $node ($nodeSet->get_nodelist){
print "\n" . $node->find('country')->string_value;
print "\n" . $node->find('indicator')->string_value;
print "\n" . $node->find('year')->string_value;
print "\n" . $node->find('value')->string_value;
exit;
}
}
}
#Build line graph based on death rate statistics and output some image file format
}
}
}
我也在研究使用 xpath 表达式“following-sibling”,但不确定如何正确使用它。例如,我有以下一组 XML 数据,我只对在死亡率数据指标之后直接拉同胞感兴趣。
<data>
<country id="AFG">Afghanistan</country>
<indicator id="SP.DYN.CDRT.IN">Death rate, crude (per 1,000 people)</indicator>
<year>2006</year>
<value>20.3410000</value>
</data>
−
<data>
<country id="AFG">Afghanistan</country>
<indicator id="SP.DYN.CDRT.IN">Death rate, crude (per 1,000 people)</indicator>
<year>2007</year>
<value>19.9480000</value>
</data>
−
<data>
<country id="AFG">Afghanistan</country>
<indicator id="SP.DYN.CDRT.IN">Death rate, crude (per 1,000 people)</indicator>
<year>2008</year>
<value>19.5720000</value>
</data>
−
<data>
<country id="AFG">Afghanistan</country>
<indicator id="IC.EXP.DOCS">Documents to export (number)</indicator>
<year>2005</year>
<value>7.0000000</value>
</data>
−
<data>
<country id="AFG">Afghanistan</country>
<indicator id="IC.EXP.DOCS">Documents to export (number)</indicator>
<year>2006</year>
<value>12.0000000</value>
</data>
−
<data>
<country id="AFG">Afghanistan</country>
<indicator id="IC.EXP.DOCS">Documents to export (number)</indicator>
<year>2007</year>
<value>12.0000000</value>
</data>
任何帮助将不胜感激!!!
【问题讨论】:
-
您需要指定您的问题。你有什么问题?请编辑您的问题以包含它。
-
对此感到抱歉...现在我已经更详细地概述了这个问题!
-
您可能不想使用 XML::XPath - 模块陈旧、缓慢且不再积极维护。我建议您切换到 XML::LibXML。 API 几乎相同,但速度更快,支持更好。
-
尝试将
my $nodeSet = $xp->find("//*");替换为:my $nodeSet = $xp->find("/*/data");或my $nodeSet = $xp->find("/*");。