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

http://www.germane-software.com/software/XML/rexml/doc/classes/REXML/Formatters.html

robert

···

2009/9/15 Charles Roper <reachme@charlesroper.co.uk>:

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

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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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__
<dict>
   <key>background</key>
   <string>#FFFFFF</string>
   <key>caret</key>
   <string>#000000</string>
   <key>foreground</key>
   <string>#000000</string>
   <key>invisibles</key>
   <string>#BFBFBF</string>
   <key>lineHighlight</key>
   <string>#00000012</string>
   <key>selection</key>
   <string>#BAD6FD</string>
</dict>

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

···

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

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

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.

---
Aaron Patterson
http://tenderlovemaking.com

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:

   <key>gutter</key>
   <string>#282828</string>
</dict>

But I can only get this:

<key>gutter</key><string>#282828</string></dict>

or this:

   <key>
     gutter
   </key>
   <string>
     #282828
   </string>
</dict>

Many thanks.

Charles

···

On 15/09/2009 08:55, Robert Klemme wrote:

Module: REXML::Formatters

robert

Aaron Patterson 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

···

--
Posted via http://www.ruby-forum.com/\.

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
"<dict><key>gutter</key><string>#282828</string></dict>"
=> <UNDEFINED> ... </>
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,"")
<dict>
  <key>gutter</key>
  <string>#282828</string>
</dict>
=> nil
irb(main):147:0>

Cheers

robert

···

2009/9/15 Charles Roper <reachme@charlesroper.co.uk>:

On 15/09/2009 08:55, Robert Klemme wrote:

Module: REXML::Formatters

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:

<key>gutter</key>
<string>#282828</string>
</dict>

But I can only get this:

<key>gutter</key><string>#282828</string></dict>

or this:

<key>
gutter
</key>
<string>
#282828
</string>
</dict>

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote:

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

···

2009/9/15 Charles Roper <reachme@charlesroper.co.uk>:

--
Posted via http://www.ruby-forum.com/\.