解析XML以获得阿尔巴尼亚人口?

时间:2013-11-21 16:01:16

标签: ruby xml nokogiri

我正在尝试学习如何使用Nokogiri并解析XML文件,但我似乎无法解决这个问题。

我有这个XML文件,其中包含有关人口,姓名,宗教信仰,通货膨胀等国家的信息:

<cia>
  <continent id='europe' 
    name='Europe'/>

  <continent id='asia' 
    name='Asia'/>

  <continent id='northAmerica' 
    name='North America'/>

  <continent id='australia' 
    name='Australia/Oceania'/>

  <continent id='southAmerica' 
    name='South America'/>

  <continent id='africa' 
    name='Africa'/>

  <country id='cid-cia-Albania' 
    continent='Europe'
    name='Albania'
    datacode='AL'
    total_area='28750'
    population='3249136'
    population_growth='1.34'
    infant_mortality='49.2'
    gdp_agri='55'
    inflation='16'
    gdp_total='4100'
    indep_date='28 11 1912'
    government='emerging democracy'
    capital='Tirane'>
    <ethnicgroups name='Greeks'>3</ethnicgroups>
    <ethnicgroups name='Albanian'>95</ethnicgroups>
    <religions name='Muslim'>70</religions>
    <religions name='Roman Catholic'>10</religions>
    <religions name='Albanian Orthodox'>20</religions>
    <borders country='cid-cia-Greece'>282</borders>
    <borders country='cid-cia-Macedonia'>151</borders>
    <borders country='cid-cia-Serbia-and-Montenegro'>287</borders>
    <coasts>Adriatic Sea</coasts>
    <coasts>Ionian Sea</coasts>
    <coasts>Serbia</coasts>
    <coasts>Montenegro</coasts>
  </country>
    .
    .
    .
</cia>

我试图通过传递国家名称作为论据来寻找一个国家,并从那里试图获得该国的人口,但我不能出于某种原因。这是我的方法:

@doc = Nokogiri::XML(File.read(file)) # get the file from the initialize method

def get_population(country)
  element = @doc.xpath("//country[@name='#{country}']")
end

所以,如果我这样做:

get_population('Albania')

我如何通过这种方法获得阿尔巴尼亚人口?目前,我得到的只是该国家的XML。

提前感谢您的所有帮助!

2 个答案:

答案 0 :(得分:3)

执行以下操作

def get_population(country)
  element = @doc.at_xpath("//country[@name='#{country}']/@population")
  element.text
end

@doc.at_xpath("//country[@name='#{country}']/@population")将为您提供Nokogiri::XML::Attr instance.Now Nokogiri::XML::Attr继承自Nokogiri::XML::Node。因此,您可以在Nokogiri::XML::Attr

的实例上使用Nokogiri::XML::Node#text方法

答案 1 :(得分:0)

使用CSS选择器使这非常简单:

require 'nokogiri'

xml = "<cia>
<continent id='europe' 
  name='Europe'/>

<continent id='asia' 
  name='Asia'/>

<continent id='northAmerica' 
  name='North America'/>

<continent id='australia' 
  name='Australia/Oceania'/>

<continent id='southAmerica' 
  name='South America'/>

<continent id='africa' 
  name='Africa'/>

<country id='cid-cia-Albania' 
  continent='Europe'
  name='Albania'
  datacode='AL'
  total_area='28750'
  population='3249136'
  population_growth='1.34'
  infant_mortality='49.2'
  gdp_agri='55'
  inflation='16'
  gdp_total='4100'
  indep_date='28 11 1912'
  government='emerging democracy'
  capital='Tirane'>
  <ethnicgroups name='Greeks'>3</ethnicgroups>
  <ethnicgroups name='Albanian'>95</ethnicgroups>
  <religions name='Muslim'>70</religions>
  <religions name='Roman Catholic'>10</religions>
  <religions name='Albanian Orthodox'>20</religions>
  <borders country='cid-cia-Greece'>282</borders>
  <borders country='cid-cia-Macedonia'>151</borders>
  <borders country='cid-cia-Serbia-and-Montenegro'>287</borders>
  <coasts>Adriatic Sea</coasts>
  <coasts>Ionian Sea</coasts>
  <coasts>Serbia</coasts>
  <coasts>Montenegro</coasts>
</country>
</cia>
"

以下是代码的要点:

doc = Nokogiri::XML(xml)
doc.at('country[name="Albania"]')['population']
# => "3249136"