Getting the DOM tree of an XML document

Does anyone know how to get an instance of a DOM (tree) XML file in Python. I am trying to compare two XML documents with eachother, which can have elements and attributes in a different order. How can I do it?

+5
source share
3 answers

Personally, when possible, I started with elementtree (preferably the C implementation that comes with the standard Python library, or lxml , but this is only important for higher speed). This is not a standard DOM, but contains the same information in a more Pythonic and convenient way. You can start with a call xml.etree.ElementTree.parsethat takes an XML source and returns a tree of elements; do this in both sources, use getrootfor each tree of elements to get its root element, and then recursively compare the elements, starting from the root.

, , DOM, , ; Python ( " ", , ) . , .

- , DOM, , , DOM , ?

, PyRXP, , ElementTree. ; , PyRXP lxml cElementTree.

+2

:

+1

XML- DOM . , , NodeComperator, node node , :

  • ?
  • ?
  • ?
  • If links to objects are expanded for comparison

Minidom is a good starting point for parsing files and is easy to use. However, the actual implementation of the comparison function for your specific application must be done by you.

0
source

All Articles