python xml dom help please

deglog spam.meplease at ntlworld.com
Wed Nov 26 14:21:40 EST 2003


Thanks for the help - this works and i understand how, and why.

jjl at pobox.com (John J. Lee) wrote in message news:<87isl89shv.fsf at pobox.com>...

> 
> Why not just
> 
> for node in list(nodeList):
>   ...
> 
> ?
> 
> 
> John

the following also works (as i intended):

from xml.dom.NodeFilter import NodeFilter

def appendToDescendant(node):
  walker.previousSibling()
  while 1:
    if walker.currentNode.hasChildNodes():
      next = walker.nextNode()
    else: break
  walker.currentNode.appendChild(node)

walker = doc.createTreeWalker(doc.documentElement,NodeFilter.SHOW_ELEMENT,
None, 0)
while 1:
  print walker.currentNode.nodeName
  if walker.currentNode.previousSibling != None:
    print "ps "+walker.currentNode.previousSibling.nodeName
    if walker.currentNode.previousSibling.nodeName != "Game":
      if walker.currentNode.previousSibling.hasChildNodes():
        appendToDescendant(walker.currentNode)
      else:
        walker.currentNode.previousSibling.appendChild(walker.currentNode)
  next = walker.nextNode()
  if next is None: break

Strangely, the line checking "Game" is needed, because this firstnode
is its own previous sibling - how can this be right?

for example with the input file:
---
<?xml version="1.0" encoding="utf-8"?>
<Game/>
---
the ouptput is:
---
Game
ps Game
<?xml version='1.0' encoding='UTF-8'?>
<Game/>




More information about the Python-list mailing list