# Indenting REXML output

**URL:** <https://www.ruby-forum.com/t/indenting-rexml-output/120029>\
**Category:** Ruby\
**Created:** [November 13, 2007, 6:02am UTC](https://www.ruby-forum.com/t/indenting-rexml-output/120029 "2007-11-13T06:02:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [November 13, 2007, 6:02am UTC](https://www.ruby-forum.com/t/indenting-rexml-output/120029/1 "2007-11-13T06:02:44Z")

</div>

Hey Guys,

Anyone know how to get REXML to generate indented XML? At the moment I  
get something like this:

Superglue is great  
stuff.

When I really want this:

 Superglue is great stuff.

Sonny.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [November 13, 2007, 6:17am UTC](https://www.ruby-forum.com/t/indenting-rexml-output/120029/2 "2007-11-13T06:17:33Z")

</div>

Quoth Sonny C.:

> Superglue is great stuff.
> 
> Sonny.

You can use a generic XML beautifier on the output. Google is your  
friend.

Regards,

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [November 13, 2007, 6:33am UTC](https://www.ruby-forum.com/t/indenting-rexml-output/120029/3 "2007-11-13T06:33:33Z")

</div>

Konrad M. wrote:

> Quoth Sonny C.:
> 
> > Superglue is great stuff.
> > 
> > Sonny.
> 
> You can use a generic XML beautifier on the output. Google is your  
> friend.
> 
> Regards,

Thanks for the tip, Konrad!

Sonny.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [November 13, 2007, 9:14am UTC](https://www.ruby-forum.com/t/indenting-rexml-output/120029/4 "2007-11-13T09:14:44Z")

</div>

2007/11/13, Sonny C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Konrad M. wrote:
> 
> > Quoth Sonny C.:
> > 
> > > Superglue is great stuff.
> > > 
> > > Sonny.
> > 
> > You can use a generic XML beautifier on the output. Google is your  
> > friend.

Not necessary. Documentation is your friend.

[http://www.germane-software.com/software/rexml/docs/tutorial.html#id2248510](http://www.germane-software.com/software/rexml/docs/tutorial.html#id2248510)

robert

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [November 13, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/indenting-rexml-output/120029/5 "2007-11-13T17:28:58Z")

</div>

Sonny C. wrote:

> Robert K. wrote:
> 
> > Not necessary. Documentation is your friend.
> > 
> > [http://www.germane-software.com/software/rexml/docs/tutorial.html#id2248510](http://www.germane-software.com/software/rexml/docs/tutorial.html#id2248510)
> > 
> > robert
> 
> Awesome, even better! Thanks Robert. I was actually reading that  
> documentation but somehow overlooked that section. Thanks again!

Do you get that formatting to work? I get a NameError exception  
(“undefined local variable or method `transitive’”) when I use a second  
parameter to doc.write. As in:

require “rexml/document”  
doc = REXML::Document.new ‘’  
doc.root.add\_element(“other”)  
doc.write( $stdout, 0 )

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [November 13, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/indenting-rexml-output/120029/6 "2007-11-13T16:49:21Z")

</div>

Robert K. wrote:

> 2007/11/13, Sonny C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Konrad M. wrote:
> > 
> > > Quoth Sonny C.:
> > > 
> > > > Superglue is great stuff.
> > > > 
> > > > Sonny.
> > > 
> > > You can use a generic XML beautifier on the output. Google is your  
> > > friend.
> 
> Not necessary. Documentation is your friend.
> 
> [http://www.germane-software.com/software/rexml/docs/tutorial.html#id2248510](http://www.germane-software.com/software/rexml/docs/tutorial.html#id2248510)
> 
> robert

Awesome, even better! Thanks Robert. I was actually reading that  
documentation but somehow overlooked that section. Thanks again!

Sonny.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [November 13, 2007, 6:24pm UTC](https://www.ruby-forum.com/t/indenting-rexml-output/120029/7 "2007-11-13T18:24:30Z")

</div>

Jari W. wrote:

> Sonny C. wrote:
> 
> > Robert K. wrote:
> > 
> > > Not necessary. Documentation is your friend.
> > > 
> > > [http://www.germane-software.com/software/rexml/docs/tutorial.html#id2248510](http://www.germane-software.com/software/rexml/docs/tutorial.html#id2248510)
> > > 
> > > robert
> > 
> > Awesome, even better! Thanks Robert. I was actually reading that  
> > documentation but somehow overlooked that section. Thanks again!
> 
> Do you get that formatting to work? I get a NameError exception  
> (“undefined local variable or method `transitive’”) when I use a second  
> parameter to doc.write. As in:
> 
> require “rexml/document”  
> doc = REXML::Document.new ‘’  
> doc.root.add\_element(“other”)  
> doc.write( $stdout, 0 )
> 
> Best regards,
> 
> Jari W.

I got the same error message as you did Jari. But I did find the REXML  
changelog, which shows the revised method to get pretty printing. This  
worked for me.

# ([http://www.germane-software.com/software/rexml/release.html](http://www.germane-software.com/software/rexml/release.html))

r1281@bean: ser | 2007-07-24 11:08:48 -0400  
Addresses ticket:85

This is a major rewrite of the XML formatting code. The XML writers have  
all  
been extracted out of the classes and put into their own class  
containers.  
This makes writing parsers easier, and cleaner.

There are three formatters, which correspond to the previous three XML  
writing  
modes:

REXML::Formatters::Default  
Prints the XML document exactly as it was parsed  
REXML::Formatters::Pretty  
Pretty prints the XML document, destroying whitespace in the document  
REXML::Formatters::Transitive  
Pretty prints the XML document, preserving whitespace

All of the write() functions have been deprecated (some are still used,  
but  
these will also go away) except the write() function on Document, which  
is left  
for convenience. To pretty print an XML document the canonical way:

# formatter = REXML::Formatters::Pretty.new( 5 ) # indent by 5 spaces formatter.write( document, output

Sonny.
