XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

REXML: Processing XML in Ruby
by Koen Vervloesem | Pages: 1, 2, 3, 4, 5

Creating and Inserting Elements and Attributes

Now we will create a small bibliography document, consisting of one biblioentry, from scratch. Here's how it goes:

irb(main):010:0> doc2 = Document.new
=> <UNDEFINED/>
irb(main):011:0> doc2.add_element("bibliography", 
                    {"id" => "philosophy"})
=> <bibliography id='philosophy'/>
irb(main):012:0> doc2.root.add_element("biblioentry")
=> <biblioentry/>
irb(main):013:0> biblioentry = doc2.root.elements[1]
=> <biblioentry/>
irb(main):014:0> author = Element.new("author")
=> <author/>
irb(main):015:0> author.add_element("firstname")
=> <firstname/>
irb(main):016:0> author.elements["firstname"].text = "Bertrand"
=> "Bertrand"
irb(main):017:0> author.add_element("surname")
=> <surname/>
irb(main):018:0> author.elements["surname"].text = "Russell"
=> "Russell"
irb(main):019:0> biblioentry.elements << author
=> <author> ... </>
irb(main):020:0> title = Element.new("title")
=> <title/>
irb(main):021:0> title.text = "The Problems of Philosophy"
=> "The Problems of Philosophy"
irb(main):022:0> biblioentry.elements << title
=> <title> ... </>
irb(main):023:0> biblioentry.elements << Element.new("pubdate")
=> <pubdate/>
irb(main):024:0> biblioentry.elements["pubdate"].text = "1912"
=> "1912"
irb(main):025:0> biblioentry.add_attribute("id", "ISBN0-19-285423-2")
=> "ISBN0-19-285423-2"
irb(main):026:0> puts doc2
<bibliography id='philosophy'>
  <biblioentry id='ISBN0-19-285423-2'>
    <author>
      <firstname>Bertrand</firstname>
      <surname>Russell</surname>
    </author>
    <title>The Problems of Philosophy</title>
    <pubdate>1912</pubdate>
  </biblioentry>
</bibliography>
=> nil

As you see, we create an empty new document and we add one element to it. This element becomes the root. The add_element method takes the name of the element as argument and an optional argument which is a hash map of name/value pairs of the attributes. So this method adds a new child to the document or an element, optionally setting attributes of the element.

You can also make a new element, like we do with the author element, and then add it afterwards to another element: if the add_element method gets an Element object, the object will be added to the parent element. Instead of the add_element method, you can also use the << method on Element.elements. The two methods return the added element.

In addition, with the method add_attribute, you can add an attribute to an existing element. The first parameter is the attribute name, the second is the attribute value. The method returns the attribute that was added. The text value of an element can easily be changed with Element.text or alternatively with the add_text method.

If you want to insert an element at a specific location, you can use the methods insert_before and insert_after:

irb(main):027:0> publisher = Element.new("publisher")
=> <publisher/>
irb(main):028:0> publishername = Element.new("publishername")
=> <publishername/>
irb(main):029:0> publishername.add_text("Oxford University Press")
=> <publishername> ... </>
irb(main):030:0> publisher << publishername
=> <publishername> ... </>
irb(main):031:0> doc2.root.insert_before("//pubdate", publisher)
=> <bibliography id='philosophy'> ... </>
irb(main):032:0> puts doc2
<bibliography id='philosophy'>
  <biblioentry id='ISBN0-19-285423-2'>
    <author>
      <firstname>Bertrand</firstname>
      <surname>Russell</surname>
    </author>
    <title>The Problems of Philosophy</title>
    <publisher>
      <publishername>Oxford University Press</publishername>
    </publisher>
    <pubdate>1912</pubdate>
  </biblioentry>
</bibliography>
=> nil

Pages: 1, 2, 3, 4, 5

Next Pagearrow