# XML Builder - why?

**URL:** <https://www.ruby-forum.com/t/xml-builder-why/69099>\
**Category:** Ruby\
**Created:** [February 16, 2006, 7:33am UTC](https://www.ruby-forum.com/t/xml-builder-why/69099 "2006-02-16T07:33:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 16, 2006, 7:33am UTC](https://www.ruby-forum.com/t/xml-builder-why/69099/1 "2006-02-16T07:33:48Z")

</div>

I see that a lot of Rubyists like using XML Builder to generate XML.  
why took this even further with markaby to generate HTML.

My question is: why? In what way is Builder any easier, clearer, more  
succint than simpling using an ERB template? I find that Builder is  
about the same length, with about the same structure, only introducing  
an extra conversion. For what?

The reason I’m asking is that I see that a lot of bright, experienced  
Ruby programmers use it, and I’m wondering if I’m missing something or  
not.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 16, 2006, 8:49am UTC](https://www.ruby-forum.com/t/xml-builder-why/69099/2 "2006-02-16T08:49:07Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I see that a lot of Rubyists like using XML Builder to generate XML.  
> why took this even further with markaby to generate HTML.
> 
> My question is: why? In what way is Builder any easier, clearer, more  
> succint than simpling using an ERB template? I find that Builder is  
> about the same length, with about the same structure, only introducing  
> an extra conversion. For what?

Uses plain Ruby. No ‘\<’ '\>" thingies. Automagic encoding of special  
characters. Less chance of error. Focus on structure and logic, not  
incidentals of the markup format.

Sure, for smallish things it may be overkill, but otherwise it is quite  
handy, especially for the angle-bracket-phobic

> The reason I’m asking is that I see that a lot of bright, experienced  
> Ruby programmers use it, and I’m wondering if I’m missing something or  
> not.

Perhaps not. Season to taste.

–  
James B.

[http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - The Journal By & For Rubyists  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 16, 2006, 2:24pm UTC](https://www.ruby-forum.com/t/xml-builder-why/69099/3 "2006-02-16T14:24:21Z")

</div>

On Thu, Feb 16, 2006 at 03:33:27PM +0900, removed\_email\_address@domain.invalid  
wrote:

> I see that a lot of Rubyists like using XML Builder to generate XML.  
> why took this even further with markaby to generate HTML.
> 
> My question is: why? In what way is Builder any easier, clearer, more  
> succint than simpling using an ERB template? I find that Builder is  
> about the same length, with about the same structure, only introducing  
> an extra conversion. For what?

I like that the ruby parser checks the well-formedness of my markup for  
me.

dave

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 16, 2006, 6:50pm UTC](https://www.ruby-forum.com/t/xml-builder-why/69099/4 "2006-02-16T18:50:45Z")

</div>

Quoting removed\_email\_address@domain.invalid:

> The reason I’m asking is that I see that a lot of bright,  
> experienced Ruby programmers use it, and I’m wondering if I’m  
> missing something or not.

Perhaps because it’s straight Ruby code – you can apply the same  
sorts of transformations/refactorings to your templates as you can  
the rest of your coe.

-mental

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 16, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/xml-builder-why/69099/5 "2006-02-16T14:42:31Z")

</div>

On Thursday 16 February 2006 1:33 am, removed\_email\_address@domain.invalid wrote:

> I see that a lot of Rubyists like using XML Builder to generate XML.  
> why took this even further with markaby to generate HTML.

Well, I don’t really use Builder for HTML, but generate quite a lot of  
XML with it. Here’s one simple use I like (make XSLT search-and-replace  
with simple YAML):

#!/usr/bin/env ruby  
require ‘yaml’  
require ‘rubygems’  
require\_gem ‘builder’, ‘~\> 1.2’

ARGV.each do |arg|  
yml = YAML::load(File.open(arg))

b = Builder::XmlMarkup.new(:target =\> STDOUT,  
:indent =\> 2)  
b.xsl :stylesheet, “version” =\>“1.0”,  
“xmlns:xsl”  
=\>“[XSLT Namespace](http://www.w3.org/1999/XSL/Transform)” do  
b.xsl :output, “method” =\>“xml”, “encoding”=\>“ascii”,  
“cdata-section-elements=\>”\_facet"

```
b.comment!("Default Rule")
b.xsl :template, "match"=>"@*|node()" do
  b.xsl :copy do
    b.xsl :"apply-templates", "select"=>"@*|node()"
  end # xsl:template
end # xsl:copy

yml.each { |outkey, outvalue|
  outvalue.each { |key, value|
    b.xsl :template, "match"=>"#{outkey}[. =

```

"`#{key}'"]" do  
b.xsl :copy, “`#{value}'”  
# b.xsl :message, “Matched #{key}”  
end  
} # end of outvalue.each  
} # end of yaml.each  
end  
end

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 16, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/xml-builder-why/69099/6 "2006-02-16T19:51:20Z")

</div>

On Fri, 2006-02-17 at 02:50 +0900, removed\_email\_address@domain.invalid wrote:

> Quoting removed\_email\_address@domain.invalid:
> 
> > The reason I’m asking is that I see that a lot of bright,  
> > experienced Ruby programmers use it, and I’m wondering if I’m  
> > missing something or not.
> 
> Perhaps because it’s straight Ruby code – you can apply the same  
> sorts of transformations/refactorings to your templates as you can  
> the rest of your coe.

Personally I use the two in different situations. Generally I prefer to  
just use ERb – I’m not especially afraid of angle brackets or anything  
like that, however, in some circumstances Builder is so much more  
sensible.

For example, I’m working on a Rails application at the moment which has  
a particular item of data organised as a tree structure. To display this  
tree structure I obviously need to use a recursive method. At first I  
tried to do this with just plain HTML code, but it soon got very messy  
with strings all over the place. I also wanted indentation so the  
generated source code was readable, which meant passing an indentation  
width to the recursive function as an argument… it was all very messy.  
Anyway, I refactored this with Builder, which handled the indentation  
for me and made the code a LOT more clean and readable because I no  
longer had strings and angle brackets left right and centre.

I’d say the above is a very logical use of Builder, although I would  
agree with you that using it for a _WHOLE_ HTML page is going a little  
too far. I guess at the end of the day though it comes down to  
preference.

Also, it goes without saying that for true XML pages (as opposed to  
HTML), Builder is far, far easier as it helps you keep pages well-formed  
and neatly laid out.
