Why can it not find my object? What am i missing here? Here is my code:
require ‘rexml/document’
include REXML
classes to represent the objects and relationships in the xml file
class Article
attr_accessor :id, :post, :archive, :ntype, :head, :blurb, :body,
:fblurb, :fimage, :att, :source, :copy, :brand
end
the base parser
class BaseXMLParser
def initialize(filename)
@raw_xml = File.open(filename).read
@newsfeed = []
end
end
DOM-based parsing
class FeedXMLParser < BaseXMLParser
def get_newsfeed
doc = Document.new(@raw_xml)
XPath.each(doc, ‘newsfeed/article’) do |article|
art = Article.new
art.id = article.attributes[‘id’]
art.post = article.attributes[‘posting_date’]
art.archive = article.attributes[‘archive_date’]
art.ntype = article.elements[‘news_type’]
art.head = article.elements[‘headline’].text
art.blurb = article.elements[‘blurb’].text
art.body = article.elements[‘body’].text
art.fblurb = article.elements[‘feature_blurb’].text
art.fimage = article.elements[‘feature_image’].text
art.att = article.elements[‘attribution’]
art.source = article.elements[‘source’].text
art.copy = article.elements[‘copyright’].text
art.brand = article.elements[‘brand’].text
@newsfeed << art
end
@newsfeed
end
end
benchmark (lets see if its working) test…
require ‘benchmark’
factory to instantiate parser instance and parse test file
klass: the class of parser to use
def get_parser(klass)
lambda do
parser_inst = klass.new(‘newsfeed.xml’)
parser_inst.get_newsfeed
end
end
docparser = get_parser(FeedXMLParser)
iterations = 10
Benchmark.bm do |x|
x.report { for i in 1…iterations; docparser.call; end }
end
display the results
art docparser.call
Here is a snippet of xml that i am trying to parse:
<?xml version="1.0"?>^M ^M ^MFYI^M ^M ^M ^M <![CDATA[(HealthDay News) -- Food poisoning often occurs after eating a meal in a large, social setting like a picnic, cookout or cafeteria. According to the U.S. National Library of Medicine, foods in these settings are often prepared early and can be left unrefrigerated for long periods, allowing bacteria to form on the food.
Food poisoning symptoms typically begin within two to six hours after eating contaminated food. Signs of food poisoning are most often vomiting, fever, chills, headache, bloody diarrhea, weakness, and severe abdominal cramps.
Treatment from a doctor is rarely necessary, unless dehydration occurs, says the NLM. To prevent dehydration, drink plenty of fluids, but avoid milk or drinks with caffeine. You should also avoid solid foods while severely nauseated. Antibiotics usually aren't needed.
]]>^M ^M