# ActiveResource xml element ordering

**URL:** <https://www.ruby-forum.com/t/activeresource-xml-element-ordering/173161>\
**Category:** Rails\
**Created:** [August 4, 2009, 7:56am UTC](https://www.ruby-forum.com/t/activeresource-xml-element-ordering/173161 "2009-08-04T07:56:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![slavix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@slavix](https://www.ruby-forum.com/u/slavix)\
**Post date:** [August 4, 2009, 7:56am UTC](https://www.ruby-forum.com/t/activeresource-xml-element-ordering/173161/1 "2009-08-04T07:56:17Z")

</div>

## Hello, I am using ActiveResource to integrate with a 3rd party service. when calling MyModel.save a POST with xml is sent. I need to make sure that xml is created with defined order of elements (the service requires it) How can I do that? Some have suggested using rxml or builder, but I am not sure how. Has anyone done this before?

SalesDocument \< ActiveResource

I need to make sure that when I call

@sales\_document.save

the order of elements in the xml is predetermined and consistent,  
which currently not the case.

POST http:///sales\_documents.xml  
body: \<?xml version="1.0" encoding="UTF-8"?\>  
  
  
  
3.0  
AD\_DASHERBOARD-200  
  
  
110  
10  
110  
Sales Lett  
10  
AG  
0000100007  
1100

---

<div class="post-metadata">

**Author:** ![slavix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@slavix](https://www.ruby-forum.com/u/slavix)\
**Post date:** [August 4, 2009, 8:14am UTC](https://www.ruby-forum.com/t/activeresource-xml-element-ordering/173161/2 "2009-08-04T08:14:23Z")

</div>

On Mon, Aug 3, 2009 at 10:56 PM, Slava  
Mikerin[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am using ActiveResource to integrate with a 3rd party service.  
> when calling MyModel.save a POST with xml is sent.  
> I need to make sure that xml is created with defined order of  
> elements (the service requires it) How can I do that?  
> Some have suggested using rxml or builder, but I am not sure how. Has  
> anyone done this before?

I haven’t, but e.g. the Builder API looks pretty straightforward:

[http://api.rubyonrails.org/classes/Builder/XmlMarkup.html](http://api.rubyonrails.org/classes/Builder/XmlMarkup.html)

:: and includes examples; maybe you should just try that and come  
back if you have a specific problem 🙂

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![slavix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@slavix](https://www.ruby-forum.com/u/slavix)\
**Post date:** [August 4, 2009, 12:07pm UTC](https://www.ruby-forum.com/t/activeresource-xml-element-ordering/173161/3 "2009-08-04T12:07:40Z")

</div>

I am stuck… here is what i tried  
class SalesDocument \< ActiveResource::Base  
def encode  
SalesDocumentsController.new.render\_to\_string :template, ‘rxml-file’  
end  
end

But that does not work. I get:

NoMethodError: protected method `render\_to\_string’ called for  
#SalesDocumentsController:0xb6fd111c

---

<div class="post-metadata">

**Author:** ![slavix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@slavix](https://www.ruby-forum.com/u/slavix)\
**Post date:** [August 4, 2009, 8:21am UTC](https://www.ruby-forum.com/t/activeresource-xml-element-ordering/173161/4 "2009-08-04T08:21:46Z")

</div>

thanks, I am not sure how to make my model convert to xml for POST  
according to builder rules…  
currently it uses a hash to represent all fields and calls hash.to\_xml  
but hash is unordered so no predictability. this is my specific problem  
🙂

Hassan S. wrote:

> On Mon, Aug 3, 2009 at 10:56 PM, Slava  
> Mikerin[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I am using ActiveResource to integrate with a 3rd party service.  
> > when calling MyModel.save a POST with xml is sent.  
> > I need to make sure that xml is created with defined order of  
> > elements (the service requires it) ï¿½How can I do that?  
> > Some have suggested using rxml or builder, but I am not sure how. Has  
> > anyone done this before?
> 
> I haven’t, but e.g. the Builder API looks pretty straightforward:
> 
> [http://api.rubyonrails.org/classes/Builder/XmlMarkup.html](http://api.rubyonrails.org/classes/Builder/XmlMarkup.html)
> 
> :: and includes examples; maybe you should just try that and come  
> back if you have a specific problem 🙂
> 
> –  
> Hassan S. ------------------------ removed\_email\_address@domain.invalid  
> twitter: @hassan

---

<div class="post-metadata">

**Author:** ![slavix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@slavix](https://www.ruby-forum.com/u/slavix)\
**Post date:** [August 4, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/activeresource-xml-element-ordering/173161/5 "2009-08-04T18:27:29Z")

</div>

Slava M. wrote:

> I am stuck… here is what i tried  
> class SalesDocument \< ActiveResource::Base  
> def encode  
> SalesDocumentsController.new.render\_to\_string :template, ‘rxml-file’  
> end  
> end
> 
> But that does not work. I get:
> 
> NoMethodError: protected method `render\_to\_string’ called for  
> #SalesDocumentsController:0xb6fd111c

Well, you’re trying to render the controller… I don’t think that’s  
what you want.

---

<div class="post-metadata">

**Author:** ![slavix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@slavix](https://www.ruby-forum.com/u/slavix)\
**Post date:** [August 4, 2009, 5:23pm UTC](https://www.ruby-forum.com/t/activeresource-xml-element-ordering/173161/6 "2009-08-04T17:23:30Z")

</div>

On Mon, Aug 3, 2009 at 11:21 PM, Slava  
Mikerin[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thanks, I am not sure how to make my model convert to xml for POST  
> according to builder rules…  
> currently it uses a hash to represent all fields and calls hash.to\_xml  
> but hash is unordered so no predictability. this is my specific problem

Are we talking about the same thing? How that even begins to relate to  
[http://api.rubyonrails.org/classes/Builder/XmlMarkup.html](http://api.rubyonrails.org/classes/Builder/XmlMarkup.html)  
escapes me…

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![slavix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@slavix](https://www.ruby-forum.com/u/slavix)\
**Post date:** [August 4, 2009, 8:44pm UTC](https://www.ruby-forum.com/t/activeresource-xml-element-ordering/173161/7 "2009-08-04T20:44:49Z")

</div>

ok. I found solution 🙂  
Here is what I did

class SalesDocument \< ActiveResource::Base  
def encode  
builder = Builder::XmlMarkup.new(:indent=\>2)  
builder.instruct!  
builder.tag!(‘sales-document’){  
builder.items(‘type’ =\> ‘array’){  
self.items.each do |item|  
builder.item{  
builder.quantity(item.quantity, “type” =\> “float”)  
builder.product(item.product)  
builder.id(item.id) if item.id  
}  
end  
}  
builder.tag!(‘sales-group’, self.sales\_group)  
builder.tag!(‘sales-office’, self.sales\_office)  
builder.tag!(‘ref-inquiry’, self.ref\_inquiry)  
builder.tag!(‘distr-channel’, self.distr\_channel)  
builder.division(self.division)  
builder.tag!(‘document-type’, self.document\_type)  
builder.customer(self.customer)  
builder.tag!(‘sales-org’, self.sales\_org)  
builder.id(self.id) if self.id  
}  
builder.target!  
end  
end

---

<div class="post-metadata">

**Author:** ![slavix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@slavix](https://www.ruby-forum.com/u/slavix)\
**Post date:** [August 4, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/activeresource-xml-element-ordering/173161/8 "2009-08-04T21:02:56Z")

</div>

Slava M. wrote:

> ok. I found solution 🙂  
> Here is what I did
> 
> class SalesDocument \< ActiveResource::Base  
> def encode  
> builder = Builder::XmlMarkup.new(:indent=\>2)

Cool. BTW, it’s more idiomatic Ruby to use do/end for multiline blocks  
and save { } for one-liners.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
