# Transmitting raw data vs. tree-structured data

**URL:** <https://www.ruby-forum.com/t/transmitting-raw-data-vs-tree-structured-data/190137>\
**Category:** Ruby\
**Created:** [June 16, 2010, 7:07pm UTC](https://www.ruby-forum.com/t/transmitting-raw-data-vs-tree-structured-data/190137 "2010-06-16T19:07:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![max-you](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@max-you](https://www.ruby-forum.com/u/max-you)\
**Post date:** [June 16, 2010, 7:07pm UTC](https://www.ruby-forum.com/t/transmitting-raw-data-vs-tree-structured-data/190137/1 "2010-06-16T19:07:36Z")

</div>

Hi, I’m trying to decide using one of two approaches getting  
tree-structured data to users.

approach #1:  
build the tree from raw data in rails model code, then transfer it via  
AJAX in XML

approach #2:  
transfer all raw data in XML form first, then use javascript to build  
the tree.

The problem with approach #1 is that I’m currently stuck doing it.  
Building the tree in model code is easy; but I’m having all sort of  
problems transferring it. (I can’t use hash.to\_xml directly since each  
key is an object, and using object.to\_xml while building to tree results  
in incorrect rendering)

The problem with approach #2 is I don’t know if javascript is power  
enough to handle “building a tree from xml, preferably using a hash that  
can have associative arrays as keys”, which is why I want to ask around  
and post on this forum.

I’m new to both Rails and javascript.

Anyone has any ideas?

---

<div class="post-metadata">

**Author:** ![max-you](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@max-you](https://www.ruby-forum.com/u/max-you)\
**Post date:** [June 16, 2010, 7:52pm UTC](https://www.ruby-forum.com/t/transmitting-raw-data-vs-tree-structured-data/190137/2 "2010-06-16T19:52:52Z")

</div>

Max Y. wrote:

> Anyone has any ideas?

Use JSON.

---

<div class="post-metadata">

**Author:** ![max-you](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@max-you](https://www.ruby-forum.com/u/max-you)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/transmitting-raw-data-vs-tree-structured-data/190137/3 "2010-06-17T16:22:10Z")

</div>

On 2010-06-16 10:52:54 -0700, Brian C. said:

> Max Y. wrote:
> 
> > Anyone has any ideas?
> 
> Use JSON.

Use XML or JSON or any other data format (JSON obviously works very  
well with javascript), but don’t let your data format design suffer  
because to\_xml doesn’t get the rendering right. Use Builder or some  
other means.

---

<div class="post-metadata">

**Author:** ![max-you](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@max-you](https://www.ruby-forum.com/u/max-you)\
**Post date:** [June 19, 2010, 11:05pm UTC](https://www.ruby-forum.com/t/transmitting-raw-data-vs-tree-structured-data/190137/4 "2010-06-19T23:05:36Z")

</div>

Thank you both for the replies. I ended up mandating all AJAX data  
transfer to use JSON format, and it works very well. Additionally, it  
turned out very easy to rewrite the stuff and add custom to\_json methods  
in rails.
