Can you give me help in the right direction? Ie: psuedo code of the recursive function? I'm trying to re-write .writexml(), but Its becoming more complicated than I thought.
I think i need a recursive function, but the problem is I'm not exactly sure on what I'm supposed to do, and so it's a mess.
def _write_subtree(self, element, writer, indent='', addindent=' ', newl='\n'): """pseudo: (i'm broke too ) foreach child of element.childNodes: print child foreach child of child.childNodes: _write_subtree() """ if element == self.root: """im at top level""" # self._write_subtree( self, element, writer, indent, addindent, newl ) element = self.root.childNodes for e in element: self._write_subtree( e, writer, indent, addindent, newl ) else: # non-root element writer.write( "<%s>" % self.root.nodeName ) for child in element.childNodes: writer.write( "%s%s" % ( addindent, str(child.nodeName) ) ) # if child.childNodes: for c in child.childNodes: print "child: %s, %s, %s" % ( c.nodeValue, c.nodeType, c.nodeValue ) writer.write(newl)def _custom_writexml(self, writer, indent='', addindent=' ', newl='\n'): """like .writexml() except dont split elements onto multiple lines if all they contain is .text element""" print "testing _jake_writexml() !" encoding = None # wrapper = _LineWrapper(writer, indent, addindent, newl, 78) # wrapper.write('<%s' % "sometag") if encoding is None: writer.write('<?xml version="1.0" ?>\n') else: writer.write('<?xml version="1.0" encoding="%s" ?>\n' % encoding) # now write tree # start out with no spacing writer.write( "<%s>" % self.root.nodeName ) self._write_subtree( self.root, writer, indent='', addindent=' ', newl='\n' ) writer.write( "</%s>" % self.root.nodeName )
This is the sample XML file that I'm tyring to generate:
<?xml version="1.0" ?><Log> <msg>game start!</msg> <graphics> <resolution h="800" w="800"/> <LOD value="1"/> </graphics> <character name="jake"> <loc x="20.3" z="-5"/> </character> <msg>game end.</msg></Log>