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
<!DOCTYPE newsfeed SYSTEM
"http://www.healthday.com/newsfeed.dtd">^M
<newsfeed>
<article id="533276" posting_date="21-Jun-2006"
archive_date="14-Jun-2007">^M<news_type>FYI</news_type>^M
<headline><![CDATA[Health Tip: Eaten Bad Food?]]></headline>^M
<blurb><![CDATA[Signs and symptoms of food poisoning
]]></blurb>^M
<byline><![CDATA[]]></byline>^M
<body><![CDATA[<p> (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.</p>
<p>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.</p>
<p>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.</p>
]]></body>^M
<attribution><![CDATA[-- Diana Kohnle]]></attribution>^M
<source><![CDATA[]]></source>^M
<feature_blurb><![CDATA[]]></feature_blurb>^M
<feature_image></feature_image>^M
<teaser><![CDATA[]]></teaser>^M
<copyright><![CDATA[Copyright (c) 2006 <a
href="http://www.healthday.com/">ScoutNews LLC</a>. All
rights
reserved.]]></copyright>^M
<brand><![CDATA[This is a story from <a
href="http://www.healthday.com">HealthDay</a>, a service of
ScoutNews,
LLC.]]></brand>^M
<url><![CDATA[id=533276]]></url>^M
</article>
</newsfeed>^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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060624/8a59ff80/attachment.html
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 Yandell
http://9cays.com/
On 24/06/2006, at 10:23 PM, Jason K. Jackson wrote:
> 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, :fbl
> urb, :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:
>
> ^M
> http://www.healthday.com/newsfeed.dtd">^M
>
> ^MFYI^M
> ^M
> ]]>^M
> ^M
> (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
> http://www.healthday.com/ ">ScoutNews LLC. All rights
reserved.]]>^M
> http://www.healthday.com">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.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails