Suggestions for Python XML library which can search and insert

L

lainedacier

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.
 
S

Steven D'Aprano

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?
 
L

lainedacier

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!
 
U

uche.ogbuji

"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/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,250
Members
46,848
Latest member
Graciela Mitchell

Latest Threads

Top