Formatting REXML

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:

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

2009/9/15 Charles R. [email protected]:

really welcome as I’ve spend ages now trying to do this one stupid thing.
http://www.germane-software.com/software/XML/rexml/doc/classes/REXML/Formatters.html

robert

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

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

2009/9/15 Charles R. [email protected]:

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

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

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

Robert K. wrote:

2009/9/15 Charles R. [email protected]:

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

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. :slight_smile:

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