Formatting REXML

C

Charles Roper

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.

Many thanks,
Charles
 
R

Robert Klemme

2009/9/15 Charles Roper said:
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.

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

robert
 
C

Charles Roper


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
 
R

Robert Klemme

2009/9/15 Charles Roper said:
Hi Robert, thanks for the link. I have already spent quite some time look= ing
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 wa= nt.

I want the last 3 lines to look like this:

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

But I can only get this:

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

or this:

=A0<key>
=A0 =A0gutter
=A0</key>
=A0<string>
=A0 =A0#282828
=A0</string>
</dict>

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

And, apparently you did not read the docs properly:

irb(main):143:0> d =3D REXML::Document.new
"<dict><key>gutter</key><string>#282828</string></dict>"
=3D> <UNDEFINED> ... </>
irb(main):144:0> f=3DREXML::Formatters::pretty.new
=3D> #<REXML::Formatters::pretty:0x1037b030 @indentation=3D2, @level=3D0,
@ie_hack=3Dfalse, @width=3D80, @compact=3Dfalse>
irb(main):145:0> f.compact=3Dtrue
=3D> true
irb(main):146:0> puts f.write(d.root,"")
<dict>
<key>gutter</key>
<string>#282828</string>
</dict>
=3D> nil
irb(main):147:0>

Cheers

robert

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

Aaron Patterson

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.

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!
 
C

Charles Roper

Robert said:
2009/9/15 Charles Roper <[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. :)
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
 
C

Charles Roper

Aaron said:
[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top