Suggestions for Python XML library which can search and insert

Discussion in 'Python' started by lainedacier@gmail.com, Jul 26, 2005.

  1. Guest

    Hi,

    I'm looking for a library that can search through an XML document tree,
    locate an element by attribute (ideally this can be done through
    XPath), and insert an element (as its child).

    Simple? Yes? ...but the code I've seen so far which does this uses
    'nested for loops' for trees which are relatively shallow compared to
    mine. So I'm wondering if you could give me some suggestions as to
    which XML library could do this without resorting to nested for loops,
    and/or better yet, what's the trend in this type of operation?

    Thanks in advance.
     
    , Jul 26, 2005
    #1
    1. Advertising

  2. Robert Kern Guest

    wrote:
    > Hi,
    >
    > I'm looking for a library that can search through an XML document tree,
    > locate an element by attribute (ideally this can be done through
    > XPath), and insert an element (as its child).


    Try lxml:

    http://codespeak.net/lxml/

    In [25]: from lxml import etree

    In [26]: nbk = etree.parse('tut-2.3.5-db.nbk')

    In [27]: log = root.xpath('//ipython-log[@id="default-log"]')[0]

    In [28]: etree.SubElement(log, 'cell')
    Out[28]: <Element cell at d668>

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Jul 26, 2005
    #2
    1. Advertising

  3. wrote:

    > Simple? Yes? ...but the code I've seen so far which does this uses
    > 'nested for loops' for trees which are relatively shallow compared to
    > mine. So I'm wondering if you could give me some suggestions as to
    > which XML library could do this without resorting to nested for loops,
    > and/or better yet, what's the trend in this type of operation?


    Why do you care if they are nested for loops?

    What is the problem you are experiencing that is caused
    by for loops?

    Does the software you've seen not work with your XML
    trees? What error do you get when you try it?

    Or are you just opposed to for loops on philosophical
    grounds?


    --
    Steven.
     
    Steven D'Aprano, Jul 26, 2005
    #3
  4. Re: how to build email message with attachment?

    On 7/26/05, praba kar <> wrote:
    > Dear All,
    >
    > Can any one let me know? How to build
    > email in python? with some some examples.
    >
    > regards
    > Prabahar


    The email module is what you need.
    http://docs.python.org/lib/module-email.html

    hth :)
     
    Tim Williams (gmail), Jul 26, 2005
    #4
  5. Guest

    > Why do you care if they are nested for loops?
    >
    > What is the problem you are experiencing that is caused
    > by for loops?
    >


    The nested loops which I speak of are highly dependent
    on the structure of the document tree. The examples which
    I've seen using DOM or ElementTree deal with shallow trees,
    but in my case, I am dealing with deeper trees--which means
    that the degree of nesting would also grow deeper if I were
    to follow suit. Also, it makes for hard to manage code.

    I'd say that lxml is exactly what I'm looking for, because
    it supports XPath and Robert Kern's example is pretty reassuring.
    Thanks Robert Kern!
     
    , Jul 26, 2005
    #5
  6. =?ISO-8859-1?Q?Gregory_Pi=F1ero?=, Jul 26, 2005
    #6
  7. Guest

    "I'm looking for a library that can search through an XML document
    tree,
    locate an element by attribute (ideally this can be done through
    XPath), and insert an element (as its child).

    Simple? Yes? ...but the code I've seen so far which does this uses
    'nested for loops' for trees which are relatively shallow compared to
    mine. "

    Amara can easily do this using XPath (complete with predicates,
    functions, etc.), without nested for loops:

    http://uche.ogbuji.net/tech/4Suite/amara/

    --
    Uche
    http://uche.ogbuji.net
     
    , Jul 30, 2005
    #7
    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.

Share This Page