lxml, comparing nodes

code_berzerker emen999 at gmail.com
Thu Jul 24 12:03:17 CEST 2008


> off the top of my head (untested):
>
>  >>> def equal(a, b):
> ...     if a.tag != b.tag or a.attrib != b.attrib:
> ...         return False
> ...     if a.text != b.text or a.tail != b.tail:
> ...         return False
> ...     if len(a) != len(b):
> ...         return False
> ...     if any(not equal(a, b) for a, b in zip(a, b)):
> ...         return False
> ...     return True
>
> this should work for arbitrary ET implementations (lxmk, xml.etree, ET,
> etc).  tweak as necessary.
>
> </F>

Thanks for help. Thats inspiring, tho not exactly what I need, coz
ignoring document order is requirement (ignoring changes in order of
different siblings of the same type, etc). I plan to try something
like that:

def xmlCmp(xmlStr1, xmlStr2):
  et1 = etree.XML(xmlStr1)
  et2 = etree.XML(xmlStr2)

  queue = []
  tmpq = deque([et1])
  tmpq2 = deque([et2])

  while tmpq:
    el = tmpq.popleft()
    tmpq.extend(el)
    queue.append(el.tag)

  while queue:
    el = queue.pop()
    foundEl = findMatchingElem(el, et2)
    if foundEl:
      et1.remove(el)
      tmpq2.remove(foundEl)
    else:
      return False

  if len(tmpq2) == 0:
    return True
  else:
    return False


def findMatchingElem(el, eTree):
  for elem in eTree:
    if elemCmp(el, elem):
      return elem
  return None


def elemCmp(el1, el2):
  pass # yet to be implemented ;)




More information about the Python-list mailing list