This is more or less direct copy / paste from documents. Hope this gives you what you want.
The most important parts call Address.parse instead of Country.parse and refer to Country fields as output.country.code instead of output.code . Then it works exactly as advertised in the Happymapper file.
#!/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
source share