Store XML format data into database

Hi all,

I am making application in Rails 3 in which I am parsing XML file and I want to store one of the XML tag in XML format into Mysql database. I have used 'TEXT' as a datatype to store XML formatted data.

I am getting the XML format Tag data but when I store it into database I am getting an error.

Can any one please help me that how can I store XML data into database?

Thanks

Show us the full error and stack trace please. Also a few lines of code around the error. We are not telepathic, at least I am not.

Colin

Hi,

Now I am not getting an error but XML values are stored like '--0 \n'.

Code is:

doc.xpath('//item').each do |tag|        @req = Hash.new        @req = tag.children[9]

       detail.create(           :my_field => tag.children[9]        )

end

And in detail_model.rb

   class Detail < ActiveRecord::Base      serialize :my_field    end

Have you used ruby-debug to break into the code here and see what tag.children[9] looks like? Have a look at the Rails Guide on debugging to see how to do this.

Colin

Hi,

Thanks for the reply.. When I puts tag.children[9] like this:

doc.xpath('//item').each do |tag|     puts tag.children[9]     @req = Hash.new     @req = tag.children[9]

    detail.create(       :my_field => tag.children[9]     ) end

I am getting the whole XML Tag. But this tag is not saving into the database. It is saving like '--0 \n'. Is there any problem with my model or Datatype into the database??

Thanks

Hi,

Thanks for the reply.. When I puts tag.children[9] like this:

doc.xpath('//item').each do |tag| puts tag.children[9] @req = Hash.new @req = tag.children[9]

detail\.create\(
  :my\_field =&gt; tag\.children\[9\]
\)

end

I am getting the whole XML Tag. But this tag is not saving into the database. It is saving like '--0 \n'. Is there any problem with my model or Datatype into the database??

I think you'll find that tag.children[9] is just an object representing the node, which doesn't have much meaning without the document it belongs to. You need to call something like to_s or to_xml on it to turn it back into a string

Fred