python xml dom help please

deglog spam.meplease at
Fri Nov 28 21:59:09 CET 2003

and-google at (Andrew Clover) wrote in message news:<2c60a528.0311261642.5478397d at>...

> > def appendToDescendant(node):
> >   walker.previousSibling()
> >   while 1:
> >     if walker.currentNode.hasChildNodes():
> >       next = walker.nextNode()
> >     else: break
> >   walker.currentNode.appendChild(node)
> Are you sure this is doing what you want? A TreeWalker's nextNode() method
> goes to an node's next matching sibling, not into its children. To go into
> the matching children you'd use TreeWalker.firstChild().


> I'm not wholly sure I understand the problem you're trying to solve. 

actually i'm trying to change the relationship 'is next sibling of' to
'is child of' throughout a document

my latest idea is to go to the end of the document, then walk it
backwards (for christmas?:-) towards this end i wrote:
walker = doc.createTreeWalker(doc.documentElement,NodeFilter.SHOW_ELEMENT,
None, 0)
while 1:
  print '1 '+walker.currentNode.nodeName
  next = walker.nextNode()
  if next is None: break
print '2 '+walker.currentNode.nodeName
which, given
<?xml version="1.0" encoding="utf-8"?>

1 Game
1 A
2 Game
foiled again. How come the current node is back at the start atfter
the loop has finished?

More information about the Python-list mailing list