Class Nokogiri::XML::Builder
In: lib/nokogiri/xml/builder.rb
Parent: Object

Nokogiri builder can be used for building XML and HTML documents.

Synopsis:

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.root {
      xml.products {
        xml.widget {
          xml.id_ "10"
          xml.name "Awesome widget"
        }
      }
    }
  end
  puts builder.to_xml

Will output:

  <?xml version="1.0"?>
  <root>
    <products>
      <widget>
        <id>10</id>
        <name>Awesome widget</name>
      </widget>
    </products>
  </root>

Builder scope

The builder allows two forms. When the builder is supplied with a block that has a parameter, the outside scope is maintained. This means you can access variables that are outside your builder. If you don‘t need outside scope, you can use the builder without the "xml" prefix like this:

  builder = Nokogiri::XML::Builder.new do
    root {
      products {
        widget {
          id_ "10"
          name "Awesome widget"
        }
      }
    }
  end

Special Tags

The builder works by taking advantage of method_missing. Unfortunately some methods are defined in ruby that are difficult or dangerous to remove. You may want to create tags with the name "type", "class", and "id" for example. In that case, you can use an underscore to disambiguate your tag name from the method call.

Here is an example of using the underscore to disambiguate tag names from ruby methods:

  @objects = [Object.new, Object.new, Object.new]

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.root {
      xml.objects {
        @objects.each do |o|
          xml.object {
            xml.type_   o.type
            xml.class_  o.class.name
            xml.id_     o.id
          }
        end
      }
    }
  end
  puts builder.to_xml

The underscore may be used with any tag name, and the last underscore will just be removed. This code will output the following XML:

  <?xml version="1.0"?>
  <root>
    <objects>
      <object>
        <type>Object</type>
        <class>Object</class>
        <id>48390</id>
      </object>
      <object>
        <type>Object</type>
        <class>Object</class>
        <id>48380</id>
      </object>
      <object>
        <type>Object</type>
        <class>Object</class>
        <id>48370</id>
      </object>
    </objects>
  </root>

Tag Attributes

Tag attributes may be supplied as method arguments. Here is our previous example, but using attributes rather than tags:

  @objects = [Object.new, Object.new, Object.new]

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.root {
      xml.objects {
        @objects.each do |o|
          xml.object(:type => o.type, :class => o.class, :id => o.id)
        end
      }
    }
  end
  puts builder.to_xml

Tag Attribute Short Cuts

A couple attribute short cuts are available when building tags. The short cuts are available by special method calls when building a tag.

This example builds an "object" tag with the class attribute "classy" and the id of "thing":

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.root {
      xml.objects {
        xml.object.classy.thing!
      }
    }
  end
  puts builder.to_xml

Which will output:

  <?xml version="1.0"?>
  <root>
    <objects>
      <object class="classy" id="thing"/>
    </objects>
  </root>

All other options are still supported with this syntax, including blocks and extra tag attributes.

Methods

cdata   new   text   to_xml  

Attributes

context  [RW]  A context object for use when the block has no arguments
doc  [RW]  The current Document object being built
parent  [RW]  The parent of the current node being built

Public Class methods

Create a new Builder object. options are sent to the top level Document that is being built.

Building a document with a particular encoding for example:

  Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    ...
  end

Public Instance methods

Create a CDATA Node with content of string

Create a Text Node with content of string

Convert this Builder object to XML

[Validate]