Ruby on Rails latest stable (v5.2.3) - 0 notes - Superclass: Builder::XmlBase
  • 1.0.0 (0)
  • 1.1.6
  • 1.2.6
  • 2.0.3
  • 2.1.0
  • 2.2.1
  • 2.3.2
  • 2.3.8
  • 3.0.0
  • 3.0.9
  • 3.1.0
  • 3.2.1
  • 3.2.8
  • 3.2.13
  • 4.0.2
  • 4.1.8
  • 4.2.1
  • 4.2.7
  • 4.2.9
  • 5.1.7
  • 5.2.3
  • 6.0.0
  • What's this?

Class deprecated or moved

This class is deprecated or moved on the latest stable version. The last existing version (v1.0.0) is shown here.

Create XML markup easily. All (well, almost all) methods sent to an XmlMarkup object will be translated to the equivalent XML markup. Any method with a block will be treated as an XML markup tag with nested markup in the block.

Examples will demonstrate this easier than words. In the following, xm is an XmlMarkup object.

  xm.em("emphasized")             # => <em>emphasized</em>
  xm.em { xmm.b("emp & bold") }   # => <em><b>emph &amp; bold</b></em>
  xm.a("A Link", "href"=>"http://onestepback.org")
                                  # => <a href="http://onestepback.org">A Link</a>
  xm.div { br }                    # => <div><br/></div>
  xm.target("name"=>"compile", "option"=>"fast")
                                  # => <target option="fast" name="compile"\>
                                  # NOTE: order of attributes is not specified.

  xm.instruct!                   # <?xml version="1.0" encoding="UTF-8"?>
  xm.html {                      # <html>
    xm.head {                    #   <head>
      xm.title("History")        #     <title>History</title>
    }                            #   </head>
    xm.body {                    #   <body>
      xm.comment! "HI"           #     <!-- HI -->
      xm.h1("Header")            #     <h1>Header</h1>
      xm.p("paragraph")          #     <p>paragraph</p>
    }                            #   </body>
  }                              # </html>


  • The order that attributes are inserted in markup tags is undefined.
  • Sometimes you wish to insert text without enclosing tags. Use the text! method to accomplish this.


      xm.div {                          # <div>
        xm.text! "line"; xm.br          #   line<br/>
        xm.text! "another line"; xmbr   #    another line<br/>
      }                                 # </div>
  • The special XML characters <, >, and & are converted to &lt;, &gt; and &amp; automatically. Use the << operation to insert text without modification.
  • Sometimes tags use special characters not allowed in ruby identifiers. Use the tag! method to handle these cases.


      xml.tag!("SOAP:Envelope") { ... }

    will produce …

      <SOAP:Envelope> ... </SOAP:Envelope>"

    tag! will also take text and attribute arguments (after the tag name) like normal markup methods. (But see the next bullet item for a better way to handle XML namespaces).

  • Direct support for XML namespaces is now available. If the first argument to a tag call is a symbol, it will be joined to the tag to produce a namespace:tag combination. It is easier to show this than describe it.
      xml.SOAP :Envelope do ... end

    Just put a space before the colon in a namespace to produce the right form for builder (e.g. "SOAP:Envelope" => "xml.SOAP :Envelope")

  • XmlMarkup builds the markup in any object (called a target) that accepts the << method. If no target is given, then XmlMarkup defaults to a string target.


      xm = Builder::XmlMarkup.new
      result = xm.title("yada")
      # result is a string containing the markup.
      buffer = ""
      xm = Builder::XmlMarkup.new(buffer)
      # The markup is appended to buffer (using <<)
      xm = Builder::XmlMarkup.new(STDOUT)
      # The markup is written to STDOUT (using <<)
      xm = Builder::XmlMarkup.new
      x2 = Builder::XmlMarkup.new(:target=>xm)
      # Markup written to +x2+ will be send to +xm+.
  • Indentation is enabled by providing the number of spaces to indent for each level as a second argument to XmlBuilder.new. Initial indentation may be specified using a third parameter.


      xm = Builder.new(:ident=>2)
      # xm will produce nicely formatted and indented XML.
      xm = Builder.new(:indent=>2, :margin=>4)
      # xm will produce nicely formatted and indented XML with 2
      # spaces per indent and an over all indentation level of 4.
      builder = Builder::XmlMarkup.new(:target=>$stdout, :indent=>2)
      builder.name { |b| b.first("Jim"); b.last("Weirich) }
      # prints:
      #     <name>
      #       <first>Jim</first>
      #       <last>Weirich</last>
      #     </name>
  • The instance_eval implementation which forces self to refer to the message receiver as self is now obsolete. We now use normal block calls to execute the markup block. This means that all markup methods must now be explicitly send to the xml builder. For instance, instead of
       xml.div { strong("text") }

    you need to write:

       xml.div { xml.strong("text") }

    Although more verbose, the subtle change in semantics within the block was found to be prone to error. To make this change a little less cumbersome, the markup block now gets the markup object sent as an argument, allowing you to use a shorter alias within the block.

    For example:

      xml_builder = Builder::XmlMarkup.new
      xml_builder.div { |xml|
Show files where this class is defined (1 file)
Register or log in to add new notes.