Parsing XML with REXML problem

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 ^M ^M ^M ^M <![CDATA[Copyright (c) 2006 ScoutNews LLC. All rights reserved.]]>^M <![CDATA[This is a story from HealthDay, a service of ScoutNews, LLC.]]>^M ^M ^M

When i run this code against the xml file i get this error:

eleven@peeps ~/NewsFeed/test $ ruby newsfeed-parser.lower.rb
newsfeed.xml
user system total real
0.833333 0.100000 0.933333 ( 0.956360)
newsfeed-parser.lower.rb:70: undefined method `art’ for main:Object
(NoMethodError)
eleven@peeps ~/NewsFeed/test $

Any idea why i am getting this error? Why can it not see my art object?

I have been on this for a few days, my deadline is around the corner so
any
advice on this is much appreciated. You can also see this posted on:
http://www.rubyonrailsforum.com/rails-code-questions/361-parsing-xml-rexml-problem.html

Thank you.

The last line of your code is:

art docparser.call

So you’re trying to call a function called ‘art’ that you haven’t
defined, hence the error. (‘art’ is a local variable in your
get_newsfeed method.)

Pete Y.
http://9cays.com/