Using REXML for XML document creation

Try :
doc = REXML::Document.new
elt = doc.add_element(“ADDRESSBOOK”)
parent = elt.add_element(“CONTACTS”)
aContact = parent.add_element(“CONTACT”)
aContact.add_attribute(“FileAs”,“Macdonald, Ian”)
aContact = parent.add_element(“CONTACT”)
aContact.add_attribute(“FileAs”,“Contact, Another”)
aContact = parent.add_element(“CONTACT”)
aContact.add_attribute(“FileAs”,“Contact, YetAnother”)

Keep the reference to the parent element, and keep adding to it.

David.

···

-----Original Message-----
From: Ian Macdonald [mailto:ian@caliban.org]
On Thu 12 Sep 2002 at 01:45:01 +0900, Matt Gushee wrote:

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”)

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

Thanks. I’m not finding this module intuitive, which is in no way
intended as a criticism of REXML. Things which are essentially very
easy aren’t coded in a way that is obvious to me.

Ian

···

On Thu 12 Sep 2002 at 10:23:12 +0900, David Naseby wrote:

Try :
doc = REXML::Document.new
elt = doc.add_element(“ADDRESSBOOK”)
parent = elt.add_element(“CONTACTS”)
aContact = parent.add_element(“CONTACT”)
aContact.add_attribute(“FileAs”,“Macdonald, Ian”)
aContact = parent.add_element(“CONTACT”)
aContact.add_attribute(“FileAs”,“Contact, Another”)
aContact = parent.add_element(“CONTACT”)
aContact.add_attribute(“FileAs”,“Contact, YetAnother”)

Keep the reference to the parent element, and keep adding to it.


Ian Macdonald | There’s something different about us –
ian@caliban.org | different from people of Europe, Africa,
> Asia … a deep and abiding belief in the
> Easter Bunny. – G. Gordon Liddy
>

In article 20020912020359.GB8028@caliban.org,

Try :
doc = REXML::Document.new
elt = doc.add_element(“ADDRESSBOOK”)
parent = elt.add_element(“CONTACTS”)
aContact = parent.add_element(“CONTACT”)
aContact.add_attribute(“FileAs”,“Macdonald, Ian”)
aContact = parent.add_element(“CONTACT”)
aContact.add_attribute(“FileAs”,“Contact, Another”)
aContact = parent.add_element(“CONTACT”)
aContact.add_attribute(“FileAs”,“Contact, YetAnother”)

Keep the reference to the parent element, and keep adding to it.

Thanks. I’m not finding this module intuitive, which is in no way
intended as a criticism of REXML. Things which are essentially very
easy aren’t coded in a way that is obvious to me.

    • XML only LOOKS obvious. I’ve only dabbled in it, but gets
      pretty complex when you want to do real things. It doesn’t
      make things easy, only standardized.
    • However, perhaps a more complex example[1] might actually make
      this simpler. I think the variable names[2] above are confusing
      the issue.

    contactNames = [ “name, one”, “name, two”, “name, three”]

    doc = REXML::Document.new
    addrbook = doc.add_element(“ADDRESSBOOK”)
    contactList = addrbook.add_element(“CONTACTS”)

    contactNames.each do |name|
    contactList.add_element(“CONTACT”).add_attribute(“FileAs”, name)
    end

    • Booker C. Bense

[1]- WARNING: untested code…

[2]- Maybe it’s just me, but that kind of use of “temp” variables
is a “code smell” to me. It seems very un-ruby like, no
offense intended.

···

Ian Macdonald ian@caliban.org wrote:

On Thu 12 Sep 2002 at 10:23:12 +0900, David Naseby wrote: