# Formatting REXML

**URL:** <https://www.ruby-forum.com/t/formatting-rexml/175533>\
**Category:** Ruby\
**Created:** [September 15, 2009, 9:49am UTC](https://www.ruby-forum.com/t/formatting-rexml/175533 "2009-09-15T09:49:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scrajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@scrajesh](https://www.ruby-forum.com/u/scrajesh)\
**Post date:** [September 15, 2009, 9:49am UTC](https://www.ruby-forum.com/t/formatting-rexml/175533/1 "2009-09-15T09:49:30Z")

</div>

Hey all,

I posted a question on StackOverflow the other day that I thought would  
be reasonably easy to answer, but actually, I got nothin’, zilch, nada.  
Rather than repeat the question here, here’s the link:

[http://bit.ly/tN7FK](http://bit.ly/tN7FK)

I don’t mind if there’s a way of formatting the XML with something other  
than REXML - whatever achieves the result is good with me. Any help  
would be really welcome as I’ve spend _ages_ now trying to do this one  
stupid thing.

Many thanks,  
Charles

---

<div class="post-metadata">

**Author:** ![scrajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@scrajesh](https://www.ruby-forum.com/u/scrajesh)\
**Post date:** [September 15, 2009, 9:56am UTC](https://www.ruby-forum.com/t/formatting-rexml/175533/2 "2009-09-15T09:56:32Z")

</div>

2009/9/15 Charles R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> really welcome as I’ve spend _ages_ now trying to do this one stupid thing.  
> [Module: REXML::Formatters](http://www.germane-software.com/software/XML/rexml/doc/classes/REXML/Formatters.html)

robert

---

<div class="post-metadata">

**Author:** ![scrajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@scrajesh](https://www.ruby-forum.com/u/scrajesh)\
**Post date:** [September 15, 2009, 10:11am UTC](https://www.ruby-forum.com/t/formatting-rexml/175533/3 "2009-09-15T10:11:45Z")

</div>

On 15/09/2009 08:55, Robert K. wrote:

> [Module: REXML::Formatters](http://www.germane-software.com/software/XML/rexml/doc/classes/REXML/Formatters.html)
> 
> robert

Hi Robert, thanks for the link. I have already spent quite some time  
looking over these formatters and I just can’t seem to get the  
formatting I want. It’s probably me being stupid, but I can’t see how to  
get the result I want.

I want the last 3 lines to look like this:

gutter  
#282828

But I can only get this:

gutter#282828

or this:

 gutter #282828 

Many thanks.

Charles

---

<div class="post-metadata">

**Author:** ![scrajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@scrajesh](https://www.ruby-forum.com/u/scrajesh)\
**Post date:** [September 15, 2009, 12:28pm UTC](https://www.ruby-forum.com/t/formatting-rexml/175533/4 "2009-09-15T12:28:07Z")

</div>

2009/9/15 Charles R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> or this:
> 
> gutter #282828

Bte, your XML is invalid. The opening tag is missing.

And, apparently you did not read the docs properly:

irb(main):143:0\> d = REXML::Document.new  
“gutter#282828”  
=\> … \</\>  
irb(main):144:0\> f=REXML::Formatters::Pretty.new  
=\> #\<REXML::Formatters::Pretty:0x1037b030 @indentation=2, @level=0,  
@ie\_hack=false, @width=80, @compact=false\>  
irb(main):145:0\> f.compact=true  
=\> true  
irb(main):146:0\> puts f.write(d.root,“”)  
  
gutter  
#282828  
  
=\> nil  
irb(main):147:0\>

Cheers

robert

---

<div class="post-metadata">

**Author:** ![scrajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@scrajesh](https://www.ruby-forum.com/u/scrajesh)\
**Post date:** [September 15, 2009, 6:32pm UTC](https://www.ruby-forum.com/t/formatting-rexml/175533/5 "2009-09-15T18:32:52Z")

</div>

On Sep 15, 2009, at 12:48 AM, Charles R. wrote:

> help would be really welcome as I’ve spend _ages_ now trying to do  
> this one stupid thing.

FTFY

require ‘rubygems’  
require ‘nokogiri’

doc = Nokogiri::XML(DATA.read) { |cfg| cfg.noblanks }

Nokogiri::XML::Builder.new do |xml|  
xml.doc = doc  
xml.parent = doc.at(‘dict’)

xml.key(‘gutter’)  
xml.string(‘#282828’)  
end

puts doc.to\_xml

**END**  
  
background  
#FFFFFF  
caret  
#000000  
foreground  
#000000  
invisibles  
#BFBFBF  
lineHighlight  
#00000012  
selection  
#BAD6FD

Doesn’t use REXML, but does output the formatting you want. Hope that  
helps!

* * *

Aaron P.  
[http://tenderlovemaking.com](http://tenderlovemaking.com)

---

<div class="post-metadata">

**Author:** ![scrajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@scrajesh](https://www.ruby-forum.com/u/scrajesh)\
**Post date:** [September 16, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/formatting-rexml/175533/6 "2009-09-16T19:50:13Z")

</div>

Aaron P. wrote:

> [Nokogiri] doesn’t use REXML, but does output the formatting you want. Hope that  
> helps!

Nokogiri looks super-nice. Much simpler than REXML from an initial  
glance. I’m going to have a play with it shortly. Already loving the  
CSS3 selectors which, for me, coming from a design background, are so  
much easier to grok than XPath. Although having said that, I was kind of  
getting into XPath since I started playing around with REXML. Cool how  
you can mix-and-match in Nokigiri.

Thanks for the head-up!

Cheers,  
Charles

---

<div class="post-metadata">

**Author:** ![scrajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@scrajesh](https://www.ruby-forum.com/u/scrajesh)\
**Post date:** [September 16, 2009, 7:37pm UTC](https://www.ruby-forum.com/t/formatting-rexml/175533/7 "2009-09-16T19:37:21Z")

</div>

Robert K. wrote:

> 2009/9/15 Charles R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Bte, your XML is invalid. The opening tag is missing.

Yeah, sorry, I didn’t give terribly clear background infomation. That  
XML I posted was just a snippet from a full XML document. Example:

[http://pastie.org/619068](http://pastie.org/619068)

I didn’t paste the whole thing because I didn’t want to clutter the post  
too much. Sorry for not explaining that more clearly. 🙂

> irb(main):146:0\> puts f.write(d.root,“”)

Awesome, this gave me the clue I needed. I was doing this:

puts out.write(tmtheme,“”)

Whereas I needed to do this:

puts out.write(tmtheme.root,“”)

Bingo! Thanks for your help Robert, it is very much appreciated.

Cheers,  
Charles
