# From\_xml not working

**URL:** <https://www.ruby-forum.com/t/from-xml-not-working/114343>\
**Category:** Rails\
**Created:** [September 11, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/from-xml-not-working/114343 "2007-09-11T16:28:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [September 11, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/from-xml-not-working/114343/1 "2007-09-11T16:28:53Z")

</div>

I’m having trouble using Hash.from\_xml, so I dropped into script/  
console to see what was happening.

> > t = Trackable.find(1)  
> > =\> #\<Activity:0xb707e5b0 @attributes={“visibility”=\>“0”, “code”=\>“K”,  
> > “type”=\>“Activity”, “disposition”=\>“2”, “id”=\>“1”,  
> > “description”=\>“Kahu”, “owner\_id”=\>“3”, “projected\_completed\_at”=\>nil,  
> > “parent\_id”=\>nil, “created\_at”=\>“2007-08-17 19:19:27”}\>
> 
> > a = t.attributes  
> > =\> {“visibility”=\>0, “code”=\>“K”, “type”=\>“Activity”, “id”=\>1,  
> > “disposition”=\>2, “description”=\>“Kahu”,  
> > “projected\_completed\_at”=\>nil, “owner\_id”=\>3, “parent\_id”=\>nil,  
> > “created\_at”=\>Fri Aug 17 19:19:27 UTC 2007}
> 
> > a.to\_xml  
> > =\> "\<?xml version=\"1.0\" encoding=\"UTF-8\"?\>\n\n \<visibility  
> > type=“integer”\>0\n `K`\n Activity\</  
> > type\>\n \<id type=“integer”\>1\n \<disposition type="integer  
> > “\>2\n Kahu\n \<projected-  
> > completed-at nil=“true”\>\n \<owner-id type=  
> > “integer”\>3\n \<parent-id nil=“true”\>\n  
> > \<created-at type=“datetime”\>2007-08-17T19:19:27Z\n\</  
> > hash\>\n”
> 
> > puts a.to\_xml

\<?xml version="1.0" encoding="UTF-8"?\> 0 `K` Activity 1 2 Kahu 3 2007-08-17T19:19:27Z =\> nil \>\> Hash.from\_xml(a.to\_xml) =\> {"hash"=\>nil}

That doesn’t work, but this does:

> > h = {‘a’ =\> ‘1’, ‘b’ =\> ‘2’}  
> > =\> {“a”=\>“1”, “b”=\>“2”}
> 
> > h.to\_xml  
> > =\> “\<?xml version=\"1.0\" encoding=\"UTF-8\"?\>\n\n 1\n  
> > **2** \n\n”
> 
> > puts h.to\_xml

\<?xml version="1.0" encoding="UTF-8"?\> 1 **2** =\> nil \>\> Hash.from\_xml(h.to\_xml) =\> {"hash"=\>{"a"=\>"1", "b"=\>"2"}}

I’m using Rails 1.2.3.

Am I doing anything wrong, or is from\_xml not working properly?

Ryan
