# REXML formatting help due to changes from 1.8.4 to 1.8.6

**URL:** <https://www.ruby-forum.com/t/rexml-formatting-help-due-to-changes-from-1-8-4-to-1-8-6/131743>\
**Category:** Ruby\
**Created:** [March 12, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/rexml-formatting-help-due-to-changes-from-1-8-4-to-1-8-6/131743 "2008-03-12T22:17:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 12, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/rexml-formatting-help-due-to-changes-from-1-8-4-to-1-8-6/131743/1 "2008-03-12T22:17:13Z")

</div>

Apparently there was an overhaul of REXML formatting between Ruby 1.8.4  
and 1.8.6.

The new formatting of XML that I’m generating with REXML is not being  
consumed correctly by the consumer anymore.

Elements containing values were written out like this in 1.8.4:

1008

Now they are written out like this:

1008

How can I get the old behavior back? I see some mention of formatters -  
so any hints on a good reference for how to use the formatters would be  
welcome.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 12, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/rexml-formatting-help-due-to-changes-from-1-8-4-to-1-8-6/131743/2 "2008-03-12T22:30:34Z")

</div>

(I see now there’s been a lot of discussion about this ;])

SOLUTION:

I changed the following method (where @document is a XML doc. created by  
REXML):

def write()  
File.open("#{@file\_path}", ‘w’) do |f|  
@document.write(f, 0)  
end  
end

to:

def write()  
formatter = REXML::Formatters::Default.new  
File.open("#{@file\_path}", ‘w’) do |f|  
formatter.write(@document, f)  
end  
end

after making sure to require ‘rexml/formatters/default’ and I’m back in  
business.

I can imagine that a lot of people have run into this, and given that  
we’re probably generating XML to communicate information to another  
system, I’m sure it has spawned some consternation. Backwards  
compatibility can be a tricky thing :).

Hope this helps,  
Wes
