[XML-SIG] newbie question on loading/saving xml files

Mariappan, MaharajanX m_mariappanX@trillium.com
Thu, 25 Oct 2001 04:06:43 -0700


Hi Folks,

I tried DOM module to load as Mark told. I'm trying to load the xml elements
to treecontrol using below code

        def LoadFileUsingDom(self, filename):
            reader = PyExpat.Reader()
            doc = reader.fromUri(filename)
            nit = doc.createNodeIterator(doc,NodeFilter.SHOW_ELEMENT,
None,0)

            ct_node = nit.nextNode()
            previous_node = None
            while ct_node:
                if ct_node.parentNode == doc:
                    self.nodeStack = [self.AddRoot(ct_node.nodeName)]
                    print "Root element is %s"%(ct_node.nodeName)
                else:
                    #id = self.AppendItem(self.nodeStack[-1],
ct_node.nodeName)
                    #self.nodeStack.append(id)
                    #if not previous_node:
                    #    self.nodeStack = self.nodeStack[:-1]
                    #noChilds=1
                    if previous_node and
self.GetItemParent(self.nodeStack[-1]) != self.GetRootItem():
                        if ct_node.parentNode.nodeName ==
previous_node.parentNode.nodeName:
                            par = self.GetItemParent(self.nodeStack[-1])
                            noChilds = self.GetChildrenCount(par, 1)
                            print "%s --> %s <==> %s
-->%s"%(previous_node.nodeName,previous_node.parentNode.nodeName,ct_node.nod
eName,ct_node.parentNode.nodeName)
                            print "childs --> %s"%(noChilds)
                            print "Par --> %s"%(self.GetItemText(par))
                            print "last node -->
%s"%(self.GetItemText(self.nodeStack[-1]))
                            print "last but one node -->
%s"%(self.GetItemText(self.nodeStack[-2]))
                            #self.nodeStack = self.nodeStack[:-noChilds]
                            print "now last node is -->
%s"%(self.GetItemText(self.nodeStack[-1]))
                            id =
self.AppendItem(self.nodeStack[-(noChilds+1)], ct_node.nodeName)
                            self.nodeStack.append(id)
                        else:
                            par = self.GetItemParent(self.nodeStack[-1])
                            noChilds = self.GetChildrenCount(par, 1)
                            print "%s --> %s <==> %s
-->%s"%(previous_node.nodeName,previous_node.parentNode.nodeName,ct_node.nod
eName,ct_node.parentNode.nodeName)
                            print "childs --> %s"%(noChilds)
                            print "Par --> %s"%(self.GetItemText(par))
                            print "last node -->
%s"%(self.GetItemText(self.nodeStack[-1]))
                            print "last but one node -->
%s"%(self.GetItemText(self.nodeStack[-2]))
                            self.nodeStack = self.nodeStack[:-(noChilds+1)]
                            print "now last node is -->
%s"%(self.GetItemText(self.nodeStack[-1]))
                            id = self.AppendItem(self.nodeStack[-1],
ct_node.nodeName)
                            self.nodeStack.append(id)
                            
                    #if previous_node:
                    #    print "%s --> %s <==> %s
-->%s"%(previous_node.nodeName,previous_node.parentNode.nodeName,ct_node.nod
eName,ct_node.parentNode.nodeName)
                    else:
                        id = self.AppendItem(self.nodeStack[-1],
ct_node.nodeName)
                        self.nodeStack.append(id)
                    previous_node = ct_node
                    
                    #print "Element %s"%(ct_node.nodeName)
                ct_node = nit.nextNode()
                                

But it seems not working fine as i except. Is there any direct way to load 
the DOM tree to tree controls

TIA
Maharajan


-----Original Message-----
From: Mark Humphrey [mailto:mark.humphrey@acm.org]
Sent: Tuesday, October 23, 2001 8:49 PM
To: Mariappan, MaharajanX
Cc: 'xml-sig@python.org'
Subject: Re: [XML-SIG] newbie question on loading/saving xml files


On Tue, Oct 23, 2001 at 02:02:49AM -0700, Mariappan, MaharajanX wrote:
> Hi Folks,
> 
> I just now start searching for documents for expat modules.
> 
> I want to 
> * load a xml file and show in GUI using python and
> * save the xml files in disk 
> 
> I went through 
> 
>  <<DOM.html.url>> 
> as Alexander told earlier, But I couldn't make out 
> 
> Pointer to any documents with examples will really help me

I'm checking my own code for this.  Seems like these are the functions that
you're wanting:

	Printer.PrintVisitor(...)
- and -
	FromXMLStream(...)

Writing works something like this:

	printer = Printer.PrintVisitor(f, 'UTF-8')
	printer.visit(domTree)

Here, f is an output file stream.  In my particular case,
	f = open(..., 'w')

You'll need to do this...
	from xml.dom.ext import Printer
...to get this object.

The FromXMLStream function takes a file stream as an input and outputs a DOM
tree, if the file stream was properly structured XML.  You get to it with:
	from xml.dom.ext.reader.Sax import FromXmlStream

After that, everything you do just consists of DOM tree manipulations, and
you should be able to find good documentation on the DOM from the w3c web
site (www.w3c.org).  PyXML has an odd mix of DOM level 1 and level 2
functions implemented, and I think I've even found a few random level 3
functions have been implemented.  Not sure what the exact pattern is,
though.

-- 
Mark "Markus" Humphrey		mark.humphrey@acm.org
http://galadriel.ath.cx:88/
GPG Public Key A54BC06F, available on www.keyserver.net
If you can't say anything bad about Microsoft, don't say anything at all.