[Zope] Change XML file with DOM API

Martijn Faassen m.faassen@vet.uu.nl
Thu, 23 Dec 1999 13:54:26 +0100


Francois-Regis CHALAOUX wrote:
> 
> >>> Martijn Faassen <m.faassen@vet.uu.nl> wrote: >>>
> 
> Could you please attempt to describe again what you're trying to
> accomplish?
> ==
> I would like, for exemple :
> 
> 1/ Add a new tag for each entry of my xml file  => In my exemple, add the <comment> tag
> 2/ Assign a value for this tag :  add the value "very good" => <comment>Very good</comment>
> 
> Sorry for my poor DOM vocabulary. FR.
> 
> this is my XML file with one entry :
> 
> <?xml version="1.0">
> <far title="Frquently Asked Restaurent">
> <entry>
> <name>La petite note</name>
> <street>rue de Thann</street>
> <number>4</number>
> <cp>67100</cp>
> <quartier>Neudorf</quartier>
> <ville>Strasbourg</ville>
> <tel>03 88 44 19 17</tel>
> <fax>03 88 34 68 54</fax>
> <email>petitenote@table.fr</email>
> <hp>www.petitenote.fr</hp>
> <closed>Monday</closed>
> <topten>Choucroute, tete de veau</topten>
> </entry>
> </far>

Okay, something like this (in an external method, you could also use
DTML directly but it's clunkier) would do the trick:

# UNTESTED CODE! This is only to point you into the right direction;
# no idea if it'll work.

# call this like this: http://mysite/mydoc/0/addComments
# (so on the 'far' node of the document)

def addComments(self, REQUEST):
    # get a list of all entries
    children = self.objectValues('entry')
    # go through them
    for child in children:
        # create comment node for child
        comment_node = self.getOwnerDocument().createElement('comment')
        # add it and get the node we just added (note that I'm not sure
the
        # latter is still necessary with the latest alpha of
XMLDocument; it
        # used to be necessary due to acquisition trickiness)
        comment_node = child.appendChild(comment_node)
        # set the text content of the comment node by adding a text node
        text_node = self.getOwnerDocument().createTextNode("My text")
        comment_node.appendChild(text_node)

Regards,

Martijn