| Who | When |
Messages | |
(not accepting new messages)
|
|
Fredrik Lundh
|
41
|
 |
|
01-26-2005 04:21 PM ET (US)
|
|
"I think there's a problem with your latest benchmark"
You're 100% correct. The benchmark is flawed, on purpose (hence the evil laugh ;-). But it was a pretty nice little piece of deceptive code, don't you think?
|
| Ramon M. Felciano
|
42
|
 |
|
01-26-2005 07:27 PM ET (US)
|
|
Has the write() code been optimized at all in cElementTree? I'm seeing very nice speedups compared with libxml2 when parsing and processing, but I lose them all when I serialize out to disk. I also note that in a simple parse/write test, cElementTree does not appear to write out the identical XML file (adds in liberal namespace info to nodes). For example, given this GraphML file as input: <?xml version="1.0" encoding="UTF-8"?> <graphml xmlns=" http://graphml.graphdrawing.org/xmlns/graphml" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xmlns:y=" http://www.yworks.com/xml/graphml"> <graph id="G" edgedefault="directed"> <node id="foo"/> </graph> </graphml> cElementTree.write() will produce the following: <ns0:graphml xmlns:ns0=" http://graphml.graphdrawing.org/xmlns/graphml"> <ns0:graph edgedefault="directed" id="G"> <ns0:node id="foo" /> </ns0:graph> </ns0:graphml> Any suggestions?
|
| David Niergarth
|
43
|
 |
|
01-26-2005 11:47 PM ET (US)
|
|
"But it was a pretty nice little piece of deceptive code, don't you think?"
Well it was certainly quite an optimization! Mixing the deceptive generator expression with timeit was especially sly. You should haul it out again on April Fool's Day.
|
Fredrik Lundh
|
44
|
 |
|
01-27-2005 06:26 AM ET (US)
|
|
"Has the write() code been optimized at all in cElementTree?"
I'm afraid not; it uses the ElementTree serialization code. With cET 1.0 out of the door, better serialization code (for both libraries) is high on my list.
(note that the XML files you included are semantically identical, from an infoset perspective. computers should not care about the transformation...)
|
| Ramon M. Felciano
|
45
|
 |
|
02-02-2005 10:12 PM ET (US)
|
|
Is it possible to use ElementTree to add processing instructions? I'm trying to add a reference to a stylesheet to my generated XML as follows:
root = Element("projectlist") pi = ProcessingInstruction("xml-stylesheet","""type="text/xsl" href="projects.xslt" """) root.insert(0,pi)
This inserts the PI under the root node; what I want is to insert it *before* the root node. I tried inserting it into an ElementTree instance, but it doesn't have an insert method and only seems to know about its root (i.e. no PIs that might preceed it) -- any suggestions?
Thanks!
Ramon
|
Fredrik Lundh
|
46
|
 |
|
02-07-2005 01:11 PM ET (US)
|
|
"Is it possible to use ElementTree to add processing instructions?"
Not really; if you want comments and PI:s to appear before the root element, you currently have to print them out yourself...
(I'm working on a new writer with better support for various things, including "invisible" elements which lets you add comments and PI:s on the document level, but it's not ready for release.)
|
| Kent Johnson
|
47
|
 |
|
02-07-2005 02:43 PM ET (US)
|
|
Just a quick thank you for cElementTree. I am writing some code to do some ad hoc matching between elements of two XML files. The total file size is almost 8MB. As I develop the matching program I process the files over and over. I can read both files and iterate all their elements in the blink of an eye.
One of the files is abstracted from a 24MB file. I used cElementTree to create the abstract, too. I tried dom4j but it choked. Text editors choke on the file too. cElementTree ate it up and was hungry for more.
Thanks!
|
| Adam Collard
|
48
|
 |
|
02-09-2005 10:05 AM ET (US)
|
|
I'm trying to use (the excellent - thank you) ElementTree to process XML files with general external entities (which refer to a 'SYSTEM' file)
My understanding is that ElementTree uses expat by default which, as a non-validating parser doesn't automatically resolve the entities. I have tried using the xmlval module from the xmlproc package and use that instead of expat but am having no success. This is the code I'm using:
----
from elementtree import ElementTree from xml.parsers.xmlproc import xmlval
p = xmlval.XMLValidator()
tree = ElementTree.parse('foo.xml',p) root = tree.getroot() root == None # True ----
foo.xml looks like this: ---- <!DOCTYPE example SYSTEM "example.dtd" [ <!ENTITY BAR SYSTEM "/home/user/bar.xml"> ]> <test> &BAR; </test> ----
Is this the kind of approach I should be taking? Are there any examples on using ElementTree with an XML document with external entities similar to above?
Thank you in advance for your assistance,
Adam
|
Fredrik Lundh
|
49
|
 |
|
02-10-2005 02:21 PM ET (US)
|
|
To use an arbitrary parser to build trees, you need to create a wrapper that maps parser events to TreeBuilder calls. See the API docs for some more details. An alternative solution is to use a validating parser that creates a light-weight model, and convert that model to an Element tree after parsing. I've posted an PyRXP(U)-based example to: http://online.effbot.org/2005_02_01_archive.htm#elementrxp
|
| Jason W.
|
50
|
 |
|
02-16-2005 08:01 PM ET (US)
|
|
Ok, maybe I am just missing something major...but I have looked all over and tried many things.
How in the heck does one go about getting the parent of a node when using elementtree? I am parsing HTML and need to grab the parent of a node that I have found (through root.findall()). Seems this would be common, but I am having no luck.
At first I was using PyXML to do what I am trying to do, but it was huge in memory (I guess because it keeps up with direct parent/sibling pointers for each node). Anyway, how would one grab the parent? Also, is there an easy way to grab the next sibling?
Thank you!
-- Jason
|
Fredrik Lundh
|
51
|
 |
|
02-17-2005 02:49 PM ET (US)
|
|
As most other container types in Python, the Element structure doesn't include parent pointers. The usual way to work around this is to operate on parents rather than children. If that's too difficult, creating a child-to-parent mapper for a given tree can be done in a single line:
parent = dict((c, p) for p in tree.getiterator() for c in p).get
Parent is now a callable that returns the parent for a given child, or None if the parent is not known.
for e in tree.findall(".//tag"): p = parent(e) ...
|
| exmu
|
52
|
 |
|
02-21-2005 05:14 AM ET (US)
|
|
Hi, I have a problem using cElementTree : importing it (on python 2.3, win32) raises the problem :
ImportError: No module named ElementTree
Does that mean cElementTree requires ElementTree ?
|
Fredrik Lundh
|
53
|
 |
|
02-21-2005 05:21 AM ET (US)
|
|
Yes (that's what "This is an add-on" and "You also need a recent version of the standard ElementTree library" is trying to say ;-)
|
| exmu
|
54
|
 |
|
02-21-2005 05:26 AM ET (US)
|
|
What can I say... Duh. Sorry.
PS : Bravo for this, I think the blatant missing feature of (c)ElementTree now is that it's not included in the standard distribution.
(or maybe that's already the case and I'm missing sth in the documentation again!)
|
| John Mudd
|
55
|
 |
|
02-23-2005 09:19 AM ET (US)
|
|
I looked at the elementTree example: http://effbot.org/zone/element-index.htmroot = Element("html") head = SubElement(root, "head") title = SubElement(head, "title") title.text = "Page Title" I looked at the Hierarchical data objects (HDO) example: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286150model=HierarchicalData() # model.person is contstruted on the fly: model.person.surname = "uwe" model.person.name = "schmitt" model.number = 1 I like how HDO builds the tree "on the fly". Is this available with elementTree? If not, is it worth considering? John
|
| infidel
|
56
|
 |
|
02-24-2005 12:11 PM ET (US)
|
|
Just curious, did you intend for ElementTree to be a pun on "Elementary", or was it just a happy coincidence?
|