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

Discussion in 'Python' started by Rajarshi, Feb 11, 2007.

  1. Rajarshi

    Rajarshi Guest

    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
     
    Rajarshi, Feb 11, 2007
    #1
    1. Advertising

  2. Re: Adding an XML fragment as a child node in a pre-existing Elementtree

    En Sun, 11 Feb 2007 15:15:21 -0300, Rajarshi <>
    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
     
    Gabriel Genellina, Feb 11, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patient Guy
    Replies:
    8
    Views:
    771
    Roy Schestowitz
    Feb 13, 2005
  2. Bryan Ax
    Replies:
    6
    Views:
    564
    Peter Flynn
    May 17, 2006
  3. Replies:
    1
    Views:
    1,224
    Joe Kesselman
    Nov 1, 2006
  4. Peter Mueller
    Replies:
    6
    Views:
    4,665
    Stefan Ram
    Jan 13, 2008
  5. User Access Approved
    Replies:
    0
    Views:
    145
    User Access Approved
    Oct 15, 2004
Loading...

Share This Page