Validating cElementTrees with lxml

R

Ryan K

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
 
J

John Machin

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

Stefan Behnel

Hi,

Ryan said:
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
 

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

Similar Threads

JavaScript Challenge: Validating Email Addresses 1
lxml 2.0 released 0
docx/lxml 6
installing lxml ? 14
XML header with lxml 2
lxml/ElementTree and .tail 30
[ANN] lxml 2.2 released 2
lxml tostring quoting too much 0

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top