469,338 Members | 8,644 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

Adding an XML fragment as a child node in a pre-existing Element tree

Hi, I'm using ElementTree for some RSS processing. The point where I
face a problem is that within an <item></itemI need to add another
child node (in addition to <linketc) which is a well-formed XML
document (Chemical Markup Language to be precise).

So my code looks like:

import cElementTree as ET

c = open('x.cml').readlines()
c = string.join(c)
cml = ET.XML(c)

Now I also have the following code:

def addItem(self, title, link, description, cml = None):
RSSitem = ET.SubElement ( self.RSSchannel, 'item' )

ET.SubElement( RSSitem, 'title' ).text = title
ET.SubElement( RSSitem, 'description' ).text = description

What I'm confused is how I can add the cml Element object that I
generated, to the RSSitem as a child node.

Do I need to manually traverse the tree of the CML document and add it
one by one to the RSSitem as a child node? Or is there a smarter way
to do this?

Any pointers would be greatly appreciated
Thanks,

Rajarshi

Feb 11 '07 #1
1 2431
En Sun, 11 Feb 2007 15:15:21 -0300, Rajarshi <ra***********@gmail.com>
escribió:
Hi, I'm using ElementTree for some RSS processing. The point where I
face a problem is that within an <item></itemI need to add another
child node (in addition to <linketc) which is a well-formed XML
document (Chemical Markup Language to be precise).

So my code looks like:

import cElementTree as ET

c = open('x.cml').readlines()
c = string.join(c)
cml = ET.XML(c)
All the above thing can be replaced by:
cml = ET.parse("x.cml")
>
Now I also have the following code:

def addItem(self, title, link, description, cml = None):
RSSitem = ET.SubElement ( self.RSSchannel, 'item' )

ET.SubElement( RSSitem, 'title' ).text = title
ET.SubElement( RSSitem, 'description' ).text = description

What I'm confused is how I can add the cml Element object that I
generated, to the RSSitem as a child node.
SubElement is just a convenience function for creating a new element and
appending it to an existing parent element. As you already have the new
subelement, just use append:

RSSitem.append(cml)

See the documentation at http://www.effbot.org/zone/element-index.htm

--
Gabriel Genellina

Feb 11 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Tim Wallace | last post: by
7 posts views Thread by Jan Eliasen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.