# XML to model conversion

**URL:** <https://www.ruby-forum.com/t/xml-to-model-conversion/71013>\
**Category:** Rails\
**Created:** [October 4, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/xml-to-model-conversion/71013 "2006-10-04T17:48:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [October 4, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/xml-to-model-conversion/71013/1 "2006-10-04T17:48:43Z")

</div>

Hi all,

I know I can use ‘to\_xml’ to get an xml representation of a model.  
However, is there an easy built-in way to convert xml(for example coming  
in as a REST web service call) back to a model? Or do I need to write my  
own method for parsing it using REXML?

Ingo

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [October 4, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/xml-to-model-conversion/71013/2 "2006-10-04T18:58:22Z")

</div>

I hope this makes sense. I am quite new to Rails, but have got my test  
app working where the ‘input’ to a model Create or update is an XML  
string. This XML only contains data relevant for the model, it is not a  
web service.

The data magically appears in the params variable.

So if the xml looks like:  
  
12345  
20.99  
500

This data will be in  
params[:product][:id]  
params[:product][:price]  
params[:product][:quantityinstock]

The slight annoyance is accessing id for the model so I do  
params[:id] = params[:product][:id]

This works for me and I use it to do updates, and create. If there is a  
better way, I’d like to know?

I don’t know if this is what you are looking for.

Andrew

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [October 4, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/xml-to-model-conversion/71013/3 "2006-10-04T20:00:27Z")

</div>

Thanks, Andrew!

I tried this, but it doesn’t seem to work for me. When my model XML is  
POSTed, what you suggested let me expect to be able to do something like  
this:

def create  
Message.create(params[:product])  
end

But that doesn’t work. The POST comes from a Flash XML class - maybe I  
need to look into exactly how Flash sends the request…

Ingo

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [October 4, 2006, 8:50pm UTC](https://www.ruby-forum.com/t/xml-to-model-conversion/71013/4 "2006-10-04T20:50:18Z")

</div>

This is how I do my create  
in class ProductsController

def create  
product = Product.new(params[:product])  
product.save

Are you using Flex or Flash?

Andrew
