[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Validating cElementTrees with lxml

ryankaskel

2/17/2008 4:24:00 AM

If I have a cElementTree.ElementTree (or the one from the Standard
Library), can I use lxml's validation features on it since it
implements the same ElementTree API?

Thanks,
Ryan
2 Answers

John Machin

2/17/2008 6:41:00 AM

0

On Feb 17, 3:23 pm, Ryan K <ryankas...@gmail.com> wrote:
> If I have a cElementTree.ElementTree (or the one from the Standard
> Library), can I use lxml's validation features on it since it
> implements the same ElementTree API?
>

I've not used lxml ... the answer depends on whether the lxml
validation features use only the published API when accessing the
tree.

Why not build your tree with lxml?

Stefan Behnel

2/17/2008 6:55:00 AM

0

Hi,

Ryan K wrote:
> If I have a cElementTree.ElementTree (or the one from the Standard
> Library), can I use lxml's validation features on it since it
> implements the same ElementTree API?

Not directly. lxml and cElementTree use different tree models internally, so
you can't just apply C-implemented features of one to the other.

Any reason you can't just use lxml /instead/ of cElementTree?

In case you really want to combine both: to validate the tree, you only need
to create an lxml tree from your ElementTree in a one-way operation, which is
easy, as this can be done through the (identical) API. Just create a new lxml
Element and then traverse the ElementTree recursively, create a new SubElement
for each child you find, and set its text, tail and attrib properties.

Something like this might work (though untested):

class TreeMigrator(object):
def __init__(ET_impl_from, ET_impl_to):
self.Element = ET_impl_to.Element
self.SubElement = ET_impl_to.SubElement

def copyChildren(self, from_parent, to_parent):
for from_child in from_parent:
tag = from_child.tag
if not isinstance(tag, basestring): # skip Comments etc.
continue
to_child = self.SubElement(
to_parent, tag, **from_child.attrib)
to_child.text = child.text
to_child.tail = child.tail
self.copyChildren(from_child, to_child)

def __call__(self, from_root):
tag = from_root.tag
to_root = self.Element(tag, **from_root.attrib)
to_root.text = from_root.text
to_root.tail = from_root.tail
if isinstance(tag, basestring): # skip Comments etc.
self.copyChildren(from_root, to_root)
return to_root

Feel free to finish it up and send it back to the list. :)

Stefan