Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Adding an XML fragment as a child node in a pre-existing Element tree

Reply
Thread Tools

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

 
 
Rajarshi
Guest
Posts: n/a
 
      02-11-2007
Hi, I'm using ElementTree for some RSS processing. The point where I
face a problem is that within an <item></item> I need to add another
child node (in addition to <link> etc) 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

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      02-11-2007
En Sun, 11 Feb 2007 15:15:21 -0300, Rajarshi <(E-Mail Removed)>
escribió:

> Hi, I'm using ElementTree for some RSS processing. The point where I
> face a problem is that within an <item></item> I need to add another
> child node (in addition to <link> etc) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
find path from one tree node to another tree node Peter Mueller Java 6 01-13-2008 02:36 AM
select nodes with child node A and child node B gplott@yahoo.com XML 1 11-01-2006 09:27 PM
problem with child text node when constraining other child node types Bryan Ax XML 6 05-17-2006 09:08 PM
Can TR element be direct child node of TABLE element? Patient Guy HTML 8 02-13-2005 03:25 PM
Toggling Content in SPAN element, One is Text Node, Other Document Fragment User Access Approved Javascript 0 10-15-2004 02:39 PM



Advertisments