473,385 Members | 1,615 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,385 software developers and data experts.

Suggestions for Python XML library which can search and insert

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 '05 #1
6 1275
la*********@gmail.com 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
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 26 '05 #2
la*********@gmail.com 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.

Jul 26 '05 #3
On 7/26/05, praba kar <pr*********@yahoo.co.in> 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 :)
Jul 26 '05 #4
> 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 '05 #5
I wrote a module to do this a while back. I put the script on my
website just now. Let me know if it helps.

http://www.blendedtechnologies.com/h...with-python/19

--
Gregory Piñero
CEO and Founder
Blended Technologies
(www.blendedtechnologies.com)
Jul 26 '05 #6
"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 29 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Henning Kage | last post by:
I'm writing a small Python library with some generic classes and interfaces, that can be used within other Python projects in our company. Would it be a better solution to compile this library into...
3
by: Parano | last post by:
Is there a Python library to access CVS? I tried to google search for it, but with "python cvs" you actually find alot of python projects that have CVS repository...
6
by: aurora | last post by:
Sometimes it is rather frustrated to use the Python Library Reference. It seems each section is hand crafted and it is up to the author decide the organization and coverage. For example the os...
0
by: Alessandro Bottoni | last post by:
Which Python library would you use for developing a small Jabber client? jabber.py (seems to be dead since 2003) http://jabberpy.sourceforge.net/ pyxmpp (looks like the "official" python...
6
by: Jonathon Blake | last post by:
All: I thought I had seen a python library/module that easily enabled one to read / write / create MSAccess databases a couple of years ago. Now, I can't find it, or any reference to such a...
10
by: bobueland | last post by:
The "Python LIbrary Reference" at http://docs.python.org/lib/contents.html seems to be an important document. I have two questions Q1. How do you search inside "Python LibraryReference" ? Does...
2
by: Allerdyce.John | last post by:
Is there a python library to parse C++ code file? If yes, can you please tell me where is it? Thank you.
1
by: m.n.summerfield | last post by:
Although a fan of Python, I find the Python Library Reference page (lib.html) very inconvenient because of its book contents-like layout. Also, some things that seem to me to belong together, such...
0
by: Jonas | last post by:
Quoting Christopher Lenz: ----------------------------------------------------------------------------------------- Babel 0.8 - Jun 20, 2007 ======================== We're proud to present the...
2
by: Sengly | last post by:
Dear all, I am looking for a python library which can cluster similar objects into their respective groups given their similarity score of each two of them. I have searched the group but I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.