2015-10-18 81 views
0

我有格式的XML飼料是這樣的:如何將自定義分析器添加到feedjira?

<league country="england"> 
<match status="15:00" time="15:00"> 
    <home name="Newcastle Utd"/> 
    <away name="Norwich"/> 
    </match> 
    <match status="15:00" time="15:00"> 
    <home name="Newcastle Utd"/> 
    <away name="Norwich"/> 
    </match> 
    <match status="19:00" time="19:00"> 
    <home name="Swansea"/> 
    <away name="Stoke City"/> 
    </match> 
</league> 

我怎樣才能在feedjira添加自定義解析器和使用它的這個飼料?由於

回答

0
require 'feedjira'; 
require './Custom.rb'; 
c = Feedjira::Feed.parse_with Custom::Custom, '<xml><title>TitlePArsed</title></xml>' 
print c.title; 

Custom.rb:

require 'feedjira'; 
module Custom 
# Parser for dealing with Atom feeds. 
class Custom 
    include SAXMachine 
    include Feedjira::FeedUtilities 
    element :title 

    def self.able_to_parse?(xml) #:nodoc: 
     true 
    end 

    def url 
    @url || (links - [feed_url]).last || links.last 
    end 

    def feed_url 
    @feed_url ||= links.first 
    end 

    def self.preprocess(xml) 
    Preprocessor.new(xml).to_xml 
    end 
end 
end 
相關問題