Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>Wikihandler#characters</code> will display the content. You could do something like:</p> <pre><code>class MyDocument &lt; Nokogiri::XML::SAX::Document attr_accessor :is_name def initialize @is_name = false end def end_document puts "the document has ended" end def start_element name, attributes = [] @is_name = name.eql?("hotelName") end def characters string string.strip! if @is_name and !string.empty? puts "Name: #{string}" end end end </code></pre> <p>However, if you want to make your life easier, I'd suggest checking out <a href="https://github.com/pauldix/sax-machine">sax-machine</a>. It adds some nice functionality and (IMHO) a friendlier interface to Nokogiri's SAX parser. Here is some sample code and specs:</p> <pre><code>require "sax-machine" require "rspec" XML = &lt;&lt;XML &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;hotels&gt; &lt;hotel&gt; &lt;hotelId&gt;1568054&lt;/hotelId&gt; &lt;hotelFileName&gt;Der_Obere_Wirt_zum_Queri&lt;/hotelFileName&gt; &lt;hotelName&gt;"Der Obere Wirt" zum Queri&lt;/hotelName&gt; &lt;rating&gt;3&lt;/rating&gt; &lt;cityId&gt;34633&lt;/cityId&gt; &lt;cityFileName&gt;Andechs&lt;/cityFileName&gt; &lt;cityName&gt;Andechs&lt;/cityName&gt; &lt;stateId&gt;212&lt;/stateId&gt; &lt;stateFileName&gt;Bavaria&lt;/stateFileName&gt; &lt;stateName&gt;Bavaria&lt;/stateName&gt; &lt;countryCode&gt;DE&lt;/countryCode&gt; &lt;countryFileName&gt;Germany&lt;/countryFileName&gt; &lt;countryName&gt;Germany&lt;/countryName&gt; &lt;imageId&gt;51498149&lt;/imageId&gt; &lt;Address&gt;Georg Queri Ring 9&lt;/Address&gt; &lt;minRate&gt;85.9800&lt;/minRate&gt; &lt;currencyCode&gt;EUR&lt;/currencyCode&gt; &lt;Latitude&gt;48.009423000000&lt;/Latitude&gt; &lt;Longitude&gt;11.214504000000&lt;/Longitude&gt; &lt;NumberOfReviews&gt;16&lt;/NumberOfReviews&gt; &lt;ConsumerRating&gt;4.25&lt;/ConsumerRating&gt; &lt;PropertyType&gt;0&lt;/PropertyType&gt; &lt;ChainID&gt;0&lt;/ChainID&gt; &lt;Facilities&gt;1|3|5|8|22|27|45|49|53|56|64|66|67|139|202|209|213|256|&lt;/Facilities&gt; &lt;/hotel&gt; &lt;hotel&gt; &lt;hotelId&gt;1658359&lt;/hotelId&gt; &lt;hotelFileName&gt;Seclusions_of_Yallingup&lt;/hotelFileName&gt; &lt;hotelName&gt;"Seclusions" of Yallingup&lt;/hotelName&gt; &lt;rating&gt;4&lt;/rating&gt; &lt;cityId&gt;72257&lt;/cityId&gt; &lt;cityFileName&gt;Yallingup&lt;/cityFileName&gt; &lt;cityName&gt;Yallingup&lt;/cityName&gt; &lt;stateId&gt;172&lt;/stateId&gt; &lt;stateFileName&gt;Western_Australia&lt;/stateFileName&gt; &lt;stateName&gt;Western Australia&lt;/stateName&gt; &lt;countryCode&gt;AU&lt;/countryCode&gt; &lt;countryFileName&gt;Australia&lt;/countryFileName&gt; &lt;countryName&gt;Australia&lt;/countryName&gt; &lt;imageId&gt;53234107&lt;/imageId&gt; &lt;Address&gt;58 Zamia Grove&lt;/Address&gt; &lt;minRate&gt;218.1825&lt;/minRate&gt; &lt;currencyCode&gt;AUD&lt;/currencyCode&gt; &lt;Latitude&gt;-33.691192000000&lt;/Latitude&gt; &lt;Longitude&gt;115.061938999999&lt;/Longitude&gt; &lt;NumberOfReviews&gt;0&lt;/NumberOfReviews&gt; &lt;ConsumerRating&gt;0&lt;/ConsumerRating&gt; &lt;PropertyType&gt;3&lt;/PropertyType&gt; &lt;ChainID&gt;0&lt;/ChainID&gt; &lt;Facilities&gt;3|6|13|14|21|22|28|39|40|41|51|53|54|56|57|58|65|66|141|191|202|204|209|210|211|292|&lt;/Facilities&gt; &lt;/hotel&gt; &lt;hotel&gt; &lt;hotelId&gt;1491947&lt;/hotelId&gt; &lt;hotelFileName&gt;1_Melrose_Blvd&lt;/hotelFileName&gt; &lt;hotelName&gt;#1 Melrose Blvd&lt;/hotelName&gt; &lt;rating&gt;5&lt;/rating&gt; &lt;cityId&gt;964&lt;/cityId&gt; &lt;cityFileName&gt;Johannesburg&lt;/cityFileName&gt; &lt;cityName&gt;Johannesburg&lt;/cityName&gt; &lt;stateId/&gt; &lt;stateFileName/&gt; &lt;stateName/&gt; &lt;countryCode&gt;ZA&lt;/countryCode&gt; &lt;countryFileName&gt;South_Africa&lt;/countryFileName&gt; &lt;countryName&gt;South Africa&lt;/countryName&gt; &lt;imageId&gt;46777171&lt;/imageId&gt; &lt;Address&gt;1 Melrose Boulevard Melrose Arch&lt;/Address&gt; &lt;minRate/&gt; &lt;currencyCode&gt;ZAR&lt;/currencyCode&gt; &lt;Latitude&gt;-26.135656000000&lt;/Latitude&gt; &lt;Longitude&gt;28.067751000000&lt;/Longitude&gt; &lt;NumberOfReviews&gt;0&lt;/NumberOfReviews&gt; &lt;ConsumerRating&gt;0&lt;/ConsumerRating&gt; &lt;PropertyType&gt;9&lt;/PropertyType&gt; &lt;ChainID&gt;0&lt;/ChainID&gt; &lt;Facilities&gt;6|7|9|11|12|15|17|18|21|32|34|39|41|42|50|51|56|58|60|140|173|202|293|296|&lt;/Facilities&gt; &lt;/hotel&gt; &lt;hotel&gt; &lt;hotelId&gt;1726938&lt;/hotelId&gt; &lt;hotelFileName&gt;1_Value_Inn_Clovis&lt;/hotelFileName&gt; &lt;hotelName&gt;#1 Value Inn Clovis&lt;/hotelName&gt; &lt;rating&gt;2&lt;/rating&gt; &lt;cityId&gt;28538&lt;/cityId&gt; &lt;cityFileName&gt;Clovis_New_Mexico&lt;/cityFileName&gt; &lt;cityName&gt;Clovis (New Mexico)&lt;/cityName&gt; &lt;stateId&gt;32&lt;/stateId&gt; &lt;stateFileName&gt;New_Mexico&lt;/stateFileName&gt; &lt;stateName&gt;New Mexico&lt;/stateName&gt; &lt;countryCode&gt;US&lt;/countryCode&gt; &lt;countryFileName&gt;United_States&lt;/countryFileName&gt; &lt;countryName&gt;United States&lt;/countryName&gt; &lt;imageId/&gt; &lt;Address&gt;1720 Mabry&lt;/Address&gt; &lt;minRate/&gt; &lt;currencyCode&gt;USD&lt;/currencyCode&gt; &lt;Latitude&gt;34.396549224853&lt;/Latitude&gt; &lt;Longitude&gt;-103.182769775390&lt;/Longitude&gt; &lt;NumberOfReviews&gt;0&lt;/NumberOfReviews&gt; &lt;ConsumerRating&gt;0&lt;/ConsumerRating&gt; &lt;PropertyType&gt;2&lt;/PropertyType&gt; &lt;ChainID&gt;0&lt;/ChainID&gt; &lt;Facilities&gt;6|7|8|18|21|22|27|41|50|52|56|222|281|292|&lt;/Facilities&gt; &lt;/hotel&gt; &lt;/hotels&gt; XML class Hotel include SAXMachine element :hotelId, :as =&gt; :id element :hotelName, :as =&gt; :name end class Wikihandler include SAXMachine elements :hotel, :as =&gt; :hotels, :class =&gt; Hotel end describe Wikihandler do before(:all) do @parser = Wikihandler.new @parser.parse XML end it "should parse the proper number of hotels" do @parser.hotels.count.should eq 4 end it "should parse the hotel id of each entry" do @parser.hotels[0].id.should eq "1568054" end it "should parse the hotel name of each entry" do @parser.hotels[0].name.should eq '"Der Obere Wirt" zum Queri' end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload