2017-02-06 16 views
5

Câu hỏi của tôi liên quan đến việc tạo đầu ra dựa trên tài liệu tại https://github.com/dam5s/happymapper là ngã ba của happymapper sử dụng nokogiri.Happymapper (ngã ba) - đầu ra từ nhiều lớp

Tôi đã sử dụng 2 ví dụ về cách xử lý tài liệu. Đây là ví dụ của tôi.

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 

sản lượng dự kiến ​​

de 
Germany 
23 

đầu ra của tôi

[email protected] ~/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

Có thể bạn cần thêm 'phần tử: housenumber, Integer,: tag =>" housenumber "' vào lớp 'Quốc gia'. Bởi vì bạn chưa định nghĩa một phương thức gọi là 'housenumber' bạn có thể gọi từ nó. –

+0

Không chắc chắn 100% ví dụ tốt chỉ không hoàn toàn rõ ràng. Trong một ví dụ, chúng tạo ra một hàm trong lớp nhưng đó là các phần tử has_many. – sayth

Trả lời

3

Đây là nhiều hay ít sao chép trực tiếp/dán từ các tài liệu. Tôi hy vọng nó sẽ giúp bạn những gì bạn muốn.

Các phần quan trọng nhất đang gọi Address.parse thay vì Country.parse và tham chiếu đến các trường Countryoutput.country.code thay vì output.code. Sau đó, nó hoạt động chính xác như được quảng cáo trong readme của 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

Tôi đã kéo nó ra vì hầu hết các mã cho tôi không đóng góp cho đầu ra. – sayth

+0

Bạn có thể xóa an toàn mọi thứ liên quan đến đường phố, mã bưu điện, housenumber và thành phố nếu bạn không sử dụng – nus

Các vấn đề liên quan