473,395 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2576
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tim Wallace | last post by:
Is there a way one can have a treeitem display the plus sign without having actually added treeitems under it as we could do in pre-.NET days? I only want to populate child nodes when the user...
2
by: Tim Wallace | last post by:
Perhaps my original post wasn't clear enough, so I'll try to reword. I only want to retrieve a node at a time for my treeview control. As the user clickes the plus sign, I will populate child...
5
by: B-Dog | last post by:
I want to be able to detect if a user has selected a parent or child node in a treeview and depending on which is clicked act accordingly based on the tag. I can get the values but don't know if...
2
by: jon|k | last post by:
hi all-- i need to do a transformation that removes duplicates (among other things). to accomplish that, i'm trying to use for-each-group, but it doesn't work. i need to select for duplicates by...
7
by: Jan Eliasen | last post by:
Hi I have some C# code that loops through an XMLDocument and needs to apply an XSLT stylesheet to each node in the XML document. My (simplified) code looks like this: XmlDocument xmldoc = new...
1
by: Tedros.G | last post by:
Hi I have an attribute the appears in both the root node and child node for example, below the attribute VERSION appears in the rood node (PRODMSG ) and a child node (OPERATION ) ================...
0
by: romcab | last post by:
Hello expert, I would like to ask how can I add a parent node and child node simultaneously. I try this code; //create parent node XmlElement ^elem =...
1
by: ramjaspari | last post by:
Hi all, I want to add childnodes to a child node(ex:ch1) and adding one more child node to the ch1 node.. and it will keep on going.. Number of nodes is NOT pre-defined.... so...
1
by: weird0 | last post by:
Here is the code that i have been using to add nodes dynamically, but I cant see any child nodes being displayed on the treeView... all i can see the root node "Network". I have been trying to...
6
by: SQACSharp | last post by:
I'm using the EnumChildWindows API with an EnumChildWndProc callback to populate the treeview. The output will be something similar to spy+ + How can I specify the parent when adding a new node...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.