# Parsing XML with REXML problem

**URL:** <https://www.ruby-forum.com/t/parsing-xml-with-rexml-problem/64341>\
**Category:** Rails\
**Created:** [June 24, 2006, 2:25pm UTC](https://www.ruby-forum.com/t/parsing-xml-with-rexml-problem/64341 "2006-06-24T14:25:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_KSJackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f19dbf/32.png) [@Jason\_KSJackson](https://www.ruby-forum.com/u/Jason_KSJackson)\
**Post date:** [June 24, 2006, 2:25pm UTC](https://www.ruby-forum.com/t/parsing-xml-with-rexml-problem/64341/1 "2006-06-24T14:25:40Z")

</div>

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](http://www.healthday.com/). All rights reserved.]]\>^M \<![CDATA[This is a story from [HealthDay](http://www.healthday.com), 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](http://www.rubyonrailsforum.com/rails-code-questions/361-parsing-xml-rexml-problem.html)

Thank you.

---

<div class="post-metadata">

**Author:** ![Jason\_KSJackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f19dbf/32.png) [@Jason\_KSJackson](https://www.ruby-forum.com/u/Jason_KSJackson)\
**Post date:** [June 24, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/parsing-xml-with-rexml-problem/64341/2 "2006-06-24T15:42:42Z")

</div>

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/](http://9cays.com/)
