# REXML Formatter issue - does each Element need formatting?

**URL:** <https://www.ruby-forum.com/t/rexml-formatter-issue-does-each-element-need-formatting/131837>\
**Category:** Ruby\
**Created:** [March 13, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/rexml-formatter-issue-does-each-element-need-formatting/131837 "2008-03-13T18:42:01Z")\
**Posts on this page:** 4\
**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 13, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/rexml-formatter-issue-does-each-element-need-formatting/131837/1 "2008-03-13T18:42:01Z")

</div>

All,

Recently upgraded to Ruby 1.8.6 and having some backwards-compatibility  
problems with REXML given it’s new formatting setup.

Basically, I have a XML document with three elements underneath the  
root. After I completely generate the XML, I use the following code to  
pretty-print it out:

```
formatter = REXML::Formatters::Default.new
File.open("#{@file_path}", 'w') do |f|
  formatter.write(@document, f)
end

```

What appears to happen, however, is that only the _first_ subordinate  
element is pretty - printed, and the rest of the contents of the root  
element are printed on one line after the first element ends.

It currently looks like this:

\<root\_elem\>  
\<first\_elem\>  
…pretty-printed contents of first\_elem…  
\</first\_elem\>\<second\_elem\>…inline contents of  
second\_elem…\</second\_elem\>\<third\_elem\>…inline contents of  
third\_elem…\</third\_elem\>  
\</root\_elem\>

The consumer of this XML is unable to parse this. Changing the consumer  
is not possible at this point, so I need to fix how this is output.

What I want to see is:

\<root\_elem\>  
\<first\_elem\>  
…pretty-printed contents of first\_elem…  
\</first\_elem\>  
\<second\_elem\>  
…pretty-printed contents of second\_elem…  
\</second\_elem\>  
\<third\_elem\>  
…pretty-printed contents of third\_elem…  
\</third\_elem\>  
\</root\_elem\>

My guess is that I will need to write out each element separately with a  
formatter, since the formatting of the document that I invoked doesn’t  
appear to propagate down into each element.

Am I on the right track?

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 13, 2008, 7:07pm UTC](https://www.ruby-forum.com/t/rexml-formatter-issue-does-each-element-need-formatting/131837/2 "2008-03-13T19:07:34Z")

</div>

Actually, it is more involved than that.

I have a XML template with the following:

\<root\_elem\>  
\<first\_elem\>  
…contents of first\_elem…  
\</first\_elem\>  
\</root\_elem\>

I then read in this XML and dynamically add in second\_elem and  
third\_elem as siblings to first\_elem. Using the default formatter  
prints them all inline. Using the pretty printer prints each element  
tag (both open and close) and the value on a separate line.

I want something in between these two extremes.

I want to print out the contents of \<second\_elem\> and \<third\_elem\> like  
so:

\<internal\_elem\>value\</internal\_elem\>

Does anyone know how to create a custom formatter for correctly provide  
formatting directives to REXML?

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 13, 2008, 7:33pm UTC](https://www.ruby-forum.com/t/rexml-formatter-issue-does-each-element-need-formatting/131837/3 "2008-03-13T19:33:12Z")

</div>

SOLUTION:

It turns out that the REXML pretty formatter has a “compact” flag that  
is undocumented, but does exactly what I want.

So this code…

require ‘rexml/document’  
require ‘rexml/formatters/pretty’

…

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

seems to work well (note the “formatter.compact = true” line).

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 16, 2008, 8:10am UTC](https://www.ruby-forum.com/t/rexml-formatter-issue-does-each-element-need-formatting/131837/4 "2008-03-16T08:10:42Z")

</div>

Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> seems to work well (note the “formatter.compact = true” line).

thanks a lot, i had the same prob 😉  
clearly i don’t understand why this isn’t the default printing mode for  
REXML…

because having :  
\<a\_node\>  
#the text for “a\_node”  
\</a\_node\>

adds to #text nodes (ie “\n”) …
