Using REXML for XML document creation

Hi,

As an REXML beginner, I’m having trouble figuring out how to nest one
element within another. Basically, I need something like this:

Right now, I can create each element sequentially, but I’m having a
hard time getting all the elements inside the
element.

Parsing an existing file is not a problem; I simply can’t work out how
to use REXML to create such a file. The documentation doesn’t
discuss embedded in this way. All the examples feature unnested
elements.

I’m obviously missing something really basic here.

Ian

···


Ian Macdonald | Immature artists imitate, mature artists
ian@caliban.org | steal. – Lionel Trilling
>
>
>

You’re probably expecting it to be harder than it is. If you’ve ever
worked with the DOM, building a document with REXML is similar but
simpler. For example:

require ‘rexml/document’

doc = REXML::Document.new
elt = doc.add_element(“ADDRESSBOOK”)
elt = elt.add_element(“CONTACTS”)
elt = elt.add_element(“CONTACT”)
elt.add_attribute(“FileAs”,“Macdonald, Ian”)

Hope this helps.

···

On Thu, Sep 12, 2002 at 12:59:42AM +0900, Ian Macdonald wrote:

As an REXML beginner, I’m having trouble figuring out how to nest one
element within another. Basically, I need something like this:

Right now, I can create each element sequentially, but I’m having a
hard time getting all the elements inside the
element.


Matt Gushee
Englewood, Colorado, USA
mgushee@havenrock.com
http://www.havenrock.com/

Thanks. It definitely did.

Ian

···

On Thu 12 Sep 2002 at 01:45:01 +0900, Matt Gushee wrote:

You’re probably expecting it to be harder than it is. If you’ve ever
worked with the DOM, building a document with REXML is similar but
simpler. For example:

require ‘rexml/document’

doc = REXML::Document.new
elt = doc.add_element(“ADDRESSBOOK”)
elt = elt.add_element(“CONTACTS”)
elt = elt.add_element(“CONTACT”)
elt.add_attribute(“FileAs”,“Macdonald, Ian”)

Hope this helps.


Ian Macdonald | University politics are vicious precisely
ian@caliban.org | because the stakes are so small. – Henry
> Kissinger
>
>

Actually, how do I add the next element? If I just use
add_element again, I get the next nested inside the first
one.

Ian

···

On Thu 12 Sep 2002 at 01:45:01 +0900, Matt Gushee wrote:

On Thu, Sep 12, 2002 at 12:59:42AM +0900, Ian Macdonald wrote:

As an REXML beginner, I’m having trouble figuring out how to nest one
element within another. Basically, I need something like this:

Right now, I can create each element sequentially, but I’m having a
hard time getting all the elements inside the
element.

You’re probably expecting it to be harder than it is. If you’ve ever
worked with the DOM, building a document with REXML is similar but
simpler. For example:

require ‘rexml/document’

doc = REXML::Document.new
elt = doc.add_element(“ADDRESSBOOK”)
elt = elt.add_element(“CONTACTS”)
elt = elt.add_element(“CONTACT”)
elt.add_attribute(“FileAs”,“Macdonald, Ian”)


Ian Macdonald | Chastity: The most unnatural of the sexual
ian@caliban.org | perversions. – Aldous Huxley
>
>
>

Well, it should be obvious that I used a bare minimum of variable names
for the sake of a minimal example. Surely you can figure out how to use
additional variable names, or a stack, or some other common device to
achieve the effect you want.

If that’s not clear yet, a little experimentation should make it so.

···

On Thu, Sep 12, 2002 at 10:19:14AM +0900, Ian Macdonald wrote:

doc = REXML::Document.new
elt = doc.add_element(“ADDRESSBOOK”)
elt = elt.add_element(“CONTACTS”)
elt = elt.add_element(“CONTACT”)
elt.add_attribute(“FileAs”,“Macdonald, Ian”)

Actually, how do I add the next element? If I just use
add_element again, I get the next nested inside the first
one.


Matt Gushee
Englewood, Colorado, USA
mgushee@havenrock.com
http://www.havenrock.com/

Actually, I’ve been having a lot of trouble doing seemingly easy
things in REXML. Perhaps that’s because REXML is my first taste of a
programmatic interface to XML.

Ian

···

On Thu 12 Sep 2002 at 10:36:07 +0900, Matt Gushee wrote:

Well, it should be obvious that I used a bare minimum of variable names
for the sake of a minimal example. Surely you can figure out how to use
additional variable names, or a stack, or some other common device to
achieve the effect you want.


Ian Macdonald | The world is an 8000 mile in diameter
ian@caliban.org | spherical pile of shit.
>
>
>

Hmm, ok … could be that it seems more obvious to me than it is. Since
I’ve been doing XML full time for 2 1/2 years, that’s entirely possible.

If the REXML documentation isn’t enough, one thing that might be helpful
is to study the DOM API, which is documented in many books and online
resources. REXML’s tree API isn’t DOM-compliant, and isn’t intended to
be (thank God!), but it’s conceptually similar enough that I think
understanding the DOM would be helpful.

The reason I say “thank God!” is that the DOM is just a big pain in the
butt: it’s a memory hog, it doesn’t handle namespaces properly, it
didn’t have ‘load()’ and ‘save()’ methods until this year … think I’ll
quit before I really get revved up. REXML is a breath of fresh air in
comparison. For example, to add a new element to an existing element,
using the W3C DOM interface you do this:

newElement = document.createElement(“foobarbaz”)
currentElement.appendChild(newElement)

whereas in REXML you just do:

currentElement.add_element(“foobarbaz”)

et cetera.

IMHO, if they DOM weren’t blessed by the W3C it would have long ago
vanished in favor of more elegant and intuitive interfaces.

···

On Thu, Sep 12, 2002 at 11:04:57AM +0900, Ian Macdonald wrote:

Well, it should be obvious that I used a bare minimum of variable names
for the sake of a minimal example. Surely you can figure out how to use
additional variable names, or a stack, or some other common device to
achieve the effect you want.

Actually, I’ve been having a lot of trouble doing seemingly easy
things in REXML. Perhaps that’s because REXML is my first taste of a
programmatic interface to XML.


Matt Gushee
Englewood, Colorado, USA
mgushee@havenrock.com
http://www.havenrock.com/

Actually, I’ve been having a lot of trouble doing seemingly easy
things in REXML. Perhaps that’s because REXML is my first taste of a
programmatic interface to XML.

I wrote an article last year about REXML and MySQL; it includes a brief
REXML tutorial.
http://www.rubyxml.com/articles/REXML/MySQL_and_REXML

It may be helpful.

James

···

Ian

Ian Macdonald | The world is an 8000 mile in diameter
ian@caliban.org | spherical pile of shit.
>
>
>

Thanks everyone for your help. I’ll check out the tutorial.

Ian

···

On Thu 12 Sep 2002 at 15:41:36 +0900, JamesBritt wrote:

I wrote an article last year about REXML and MySQL; it includes a brief
REXML tutorial.
http://www.rubyxml.com/articles/REXML/MySQL_and_REXML

It may be helpful.


Ian Macdonald | “Life is like a buffet; it’s not good but
ian@caliban.org | there’s plenty of it.”
>
>
>