python xml dom help please

deglog spam.meplease at ntlworld.com
Sun Nov 23 11:13:24 EST 2003


Apologies if this post appears more than once.

The file - 

---------------
<?xml version="1.0" encoding="utf-8"?>
<Game><A/><B/><C/></Game>
---------------

is processed by this program - 

---------------
#!/usr/bin/env python

from xml.dom.ext.reader  import PyExpat
from xml.dom.ext         import PrettyPrint

import sys

def deepen(nodeList):
  for node in nodeList:
    print(node.nodeName)
    if node.previousSibling != None:
      if node.previousSibling.nodeType == node.ELEMENT_NODE:
        if node.previousSibling.hasChildNodes():
          print("has children")
          node.previousSibling.lastChild.appendChild(node)
        else:
          node.previousSibling.appendChild(node)      
    deepen(node.childNodes)

# get DOM object
reader = PyExpat.Reader()
doc = reader.fromUri(sys.argv[1])

# call func
deepen(doc.childNodes)

# display altered document
PrettyPrint(doc)
---------------

which outputs the following - 

---------------
Game
Game
A
B
<?xml version='1.0' encoding='UTF-8'?>
<Game>
  <A>
    <B/>
  </A>
  <C/>
</Game>

---------------

Can anybody explain why the line 'print(node.nodeName)' never prints 'C'?

Also, why 'has children' is never printed?

I am trying to output

---------------
<?xml version='1.0' encoding='UTF-8'?>
<Game>
  <A>
    <B>
      <C/> 
    </B>    
  </A>
</Game>
---------------

I know there are easier ways to do this, but i want to do it using dom.

Thanks in advance.




More information about the Python-list mailing list