0

I am trying to convert a random string (which is build in XML format) in to an xml, so I can apply the "to_hash" function to it.

This is what I have:

model = live_requests[3]
parser = XML::Parser.string(model)
model_xml = parser.parse

puts model.to_hash

Now why am I getting an error when 'model_xml' should be an XML file?

I am using LibXML by the way. http://libxml.rubyforge.org/rdoc/index.html

Test Test
  • 2,831
  • 8
  • 44
  • 64
  • 4
    What is your input? What is the error? Can you provide a little more information on *why* you want to do this? You'll get better answers. – Mark Thomas Nov 07 '12 at 12:28

1 Answers1

0

Libxml does not support the to_hash method. If you are looking for a way to do this that doesn't require traversing XML nodes and bulding the hash manually you should take a look at Nori.

Nori.parse("<tag>This is the contents</tag>")
# => { 'tag' => 'This is the contents' }

If you want to learn how to traverse Libxml's node trees take a look at the answer to this question.

Community
  • 1
  • 1
lastcanal
  • 2,145
  • 14
  • 17