Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strip error #67

Closed
MortalCatalyst opened this issue Feb 9, 2015 · 3 comments
Closed

strip error #67

MortalCatalyst opened this issue Feb 9, 2015 · 3 comments

Comments

@MortalCatalyst
Copy link

I have created a nokogiri document 'doc' each time I attempt to parse it with Nori I get an error.

'[63] pry(main)> parser = Nori.new
=> #<Nori:0x00000004f34b30
@options=
{:strip_namespaces=>false,
:delete_namespace_attributes=>false,
:convert_tags_to=>nil,
:convert_attributes_to=>nil,
:advanced_typecasting=>true,
:convert_dashes_to_underscores=>true,
:parser=>:nokogiri}>
[64] pry(main)> parser.parse(@doc)
NoMethodError: undefined method strip' for #<Nokogiri::XML::Document:0x00000002103ae0> from /var/lib/gems/1.9.1/gems/nori-2.4.0/lib/nori.rb:42:inparse'
[65] pry(main)> parser.parse(doc)
NoMethodError: undefined method strip' for #<Nokogiri::XML::Document:0x25decf4 name="document"> from /var/lib/gems/1.9.1/gems/nori-2.4.0/lib/nori.rb:42:inparse'

and even with a tiny called tiny.xml

<Races>
<Race RaceNumber="1"><NameRaceFull>Foo</NameRaceFull></Race>
<Race RaceNumber="2"><NameRaceFull>Goo</NameRaceFull></Race>
<Race RaceNumber="3"><NameRaceFull>Hoo</NameRaceFull></Race>
</Races>

[68] pry(main)> newdoc = Nokogiri::XML("tiny.xml")
=> #(Document:0x24ed034 { name = "document" })
[69] pry(main)> parser.parse(newdoc)
NoMethodError: undefined method strip' for #<Nokogiri::XML::Document:0x24ed034 name="document"> from /var/lib/gems/1.9.1/gems/nori-2.4.0/lib/nori.rb:42:inparse'
[70] pry(main)>

Am I doing something wrong?

@tjarratt
Copy link
Contributor

tjarratt commented Feb 9, 2015

Hey @MortalCatalyst, I think you might be doing this a little wrong. Nori#parse expects a string as its input -- you should be able to see this in the examples the README includes.

Maybe it makes sense for Nori to raise a better error here, so it's more obvious why this is failing?

@MortalCatalyst
Copy link
Author

OK so I just need to do it more like this
def get_file_as_string(filename)
data = ''
f = File.open(filename, "r")
f.each_line do |line|
data += line
end
return data
end

MAIN

xml_data = get_file_as_string 'Pizza.hbm.xml'

print out the string

puts xml_data

Which I got from here

http://alvinalexander.com/blog/post/ruby/ruby-method-read-in-entire-file-as-string

@tjarratt
Copy link
Contributor

tjarratt commented Feb 9, 2015

Ah, even better you could probably use the builtin method File.read to read your file in one line.

require 'nori'

xml_data = File.read('Pizza.hbm.xml')
puts xml_data

hash = Nori.new.parse(xml_data)

puts hash.inspect

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants