Parsing XML to get the population of Albania?

别说谁变了你拦得住时间么 提交于 2019-12-25 08:56:33

问题


I am trying to learn how to use Nokogiri and parse XML files, however I can't seem to get past this issue I am having.

I have this XML file with information about countries such as population, name, religion, inflation etc.:

<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>

I am trying to find a country by passing in the name of the country as an argument, and, from there, trying to get the population of the country, but I can't for some reason. Here is my method:

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

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

So if I do:

get_population('Albania')

How can I get this method to get the population for Albania? Currently all I get is the XML for that country.

Thanks for all the help in advance!


回答1:


Do as below

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

@doc.at_xpath("//country[@name='#{country}']/@population") will give you Nokogiri::XML::Attr instance.Now Nokogiri::XML::Attr inherits from Nokogiri::XML::Node. So you can use Nokogiri::XML::Node#text method, on the instance of Nokogiri::XML::Attr.




回答2:


Using CSS selectors makes this very straight-forward:

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>
"

Here's the gist of the code:

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


来源:https://stackoverflow.com/questions/20125850/parsing-xml-to-get-the-population-of-albania

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!