user3037484
user3037484

Reputation: 25

undefined method `valid_xml?'

I am new to ruby. I keep getting undefined method 'valid_xml'? What am I doing wrong?

require 'rexml/document'
include REXML
begin
    good_xml = %{
     <groceries>

      <bread>Wheat</bread>
      <bread>Quadrotriticale</bread>
     </groceries>}
puts(good_xml)
valid_xml?(good_xml)
puts("good read")
rescue Exception => e
puts(e.message)
end

and here is the output

-bash-4.1$ ruby test.rb

     <groceries>

      <bread>Wheat</bread>
      <bread>Quadrotriticale</bread>
     </groceries>
undefined method `valid_xml?' for main:Object

Here is the ruby version I have

ruby 1.8.7 (2009-12-24 patchlevel 248) [x86_64-linux]

Upvotes: 0

Views: 49

Answers (1)

Lukas_Skywalker
Lukas_Skywalker

Reputation: 2070

valid_xml? is not defined in REXML (and neither did you define it). You could define it like this:

def valid_xml?(xml)
   REXML::Document.new(xml)
   true
rescue REXML::ParseException
   false
end

Upvotes: 3

Related Questions