My question relates to creating output based on the documentation at https://github.com/dam5s/happymapper which is the fork of happymapper using nokogiri.
I have used 2 examples in toying with documentation. This is my example.
xml_doc = <<EOF <address location='home'> <street>Milchstrasse</street> <street>Another Street</street> <housenumber>23</housenumber> <postcode>26131</postcode> <city>Oldenburg</city> <country code="de">Germany</country> </address> EOF class Address include HappyMapper tag 'address' element :housenumber, Integer, :tag => "housenumber" end class Country include HappyMapper tag 'country' attribute :code, String content :name, String end outputs = Country.parse(xml_doc) outputs.each do |output| puts output.code puts output.name puts output.housenumber end Expected output
de Germany 23 My output
sayth@sayth-E6410 ~/race (master●)$ ruby read_race.rb [ruby-2.4.0p0] de Germany read_race.rb:49:in `block in <main>': undefined method `housenumber' for #<Country:0x0055e55facf798 @code="de", @name="Germany"> (NoMethodError) from read_race.rb:46:in `each' from read_race.rb:46:in `<main>' 1 Answers
Answers 1
This is more or less direct copy/paste from the docs. I hope it gets you what you want.
The most important parts are calling Address.parse instead of Country.parse and referring to the Country fields as output.country.code instead of output.code. Then it works exactly as advertised in the readme of Happymapper.
#!/usr/bin/env ruby require 'happymapper' ADDRESS_XML_DATA = <<XML <root> <address location='home'> <street>Milchstrasse</street> <street>Another Street</street> <housenumber>23</housenumber> <postcode>26131</postcode> <city>Oldenburg</city> <country code="de">Germany</country> </address> </root> XML class Country include HappyMapper tag 'country' attribute :code, String content :name, String end class Address include HappyMapper tag 'address' has_many :streets, String, :tag => 'street' def streets @streets.join('\n') end element :postcode , String , :tag => 'postcode' element :housenumber, String , :tag => 'housenumber' element :city , String , :tag => 'city' element :country , Country, :tag => 'country' end outputs = Address.parse(ADDRESS_XML_DATA) outputs.each do |output| puts output.country.code puts output.country.name puts output.housenumber end
0 comments:
Post a Comment