[XML-SIG] Re: Could somebody help me?

Prasad PS prasad_st at beceem.com
Wed Feb 2 10:40:00 CET 2005


Hi,
 Using the code below, I have created a file "TempView.xml". Could
anybody tell me how to append another "Employee" to the existing xml
file?
Here's the snippet I did to create an xml file.

import getopt
import os
import string
import sys
import xml.dom.minidom
from xml.dom.minidom import Node
from xml.dom import minidom
from xml.dom.ext.reader.Sax2 import FromXmlStream
from xml.dom.ext.reader import Sax2
from xml.dom.ext import PrettyPrint
from xml.dom.DOMImplementation import implementation
import xml.sax.writer
import xml.utils

class LogView:
    def __init__(self):
        self.LogViewFile = open("TempView.xml",'w')
        self.document = implementation.createDocument(None,None,None)
        self.logViews = self.document.createElement("EmpDetails")
        self.document.appendChild(self.logViews)
    
    def createViewFile(self):
        self.logViews.appendChild(doc.createTextNode("\n  "))
        logdetail = doc.createElement("Address")
        self.logViews.appendChild(logdetail)
    
        logdetail.appendChild(doc.createTextNode("\n   "))
        
        tcidNode = doc.createElement("Name")
        tcidNode.appendChild(doc.createTextNode("Prasad"))
        logdetail.appendChild(tcidNode)
        logdetail.appendChild(doc.createTextNode("\n "))
        
        grpNode = doc.createElement("Age")
        grpNode.appendChild(doc.createTextNode("28"))
        logdetail.appendChild(grpNode)
        logdetail.appendChild(doc.createTextNode("\n  "))
        
        
    def finalStep(self):
        t = self.document.createTextNode("\n")
        self.logViews.appendChild(t)
        PrettyPrint(self.document, self.LogViewFile)
        self.LogViewFile.write("\n")  





Prasad.p.s.


-----Original Message-----
From: Uche Ogbuji [mailto:Uche.Ogbuji at fourthought.com] 
Sent: Friday, January 28, 2005 8:43 PM
To: Prasad PS
Cc: XML-SIG
Subject: RE: [XML-SIG] Re: Could somebody help me?

On Fri, 2005-01-28 at 15:48 +0530, Prasad PS wrote:
> Sure, here is the code
> 
> In the code below, what I am doing is - I am opening an xml file and
> appending a node to the root document. Then I add this root document
to
> the xml file
> fp = open (string.strip(self.cnfDtls.GetLogFilePath()), 'w')
> xml.dom.ext.PrettyPrint(doc, self.xmlFile)
> self.xmlFile.write("\n") 
> fp.close().

So you tried the first choice (PyXML) rather than the second (Amara).
OK.  You were not clear on that.

Your first problem is that you're using xml.dom.ext.reader.FromXmlStream
rather than 

from xml.dom import minidom
doc = minidom.parse(string.strip(self.cnfDtls.GetLogFilePath()))

...

doc.toprettyxml() (rather than xml.dom.ext.PrettyPrint)

That's the fault of the PyXML docs, which should really be updated.

Side question: you mean you're appending a node to the document element,
right?  Not the root document.  The latter would result in an invalid
XML document entity.

In the code you posted, it looks as if you only append to subsidiary
nodes, so that should be OK.

Even using 4DOM, your general approach should work, and I've used it
oftentimes before (in the far-off past), with no problem, so I wonder:
Are you sure self.xmlFile is "empty" at the point of the
xml.dom.ext.PrettyPrint?

If so, I suggest you whittle down a test case that reveals the apparent
bug, and post data and complete, runnable code (preferably after
switching to minidom).  If it seems a clear bug, you can use the PyXML
bug tracker.


-- 
Uche Ogbuji                                    Fourthought, Inc.
http://uche.ogbuji.net    http://4Suite.org    http://fourthought.com
Use CSS to display XML -
http://www.ibm.com/developerworks/edu/x-dw-x-xmlcss-i.html
Introducing the Amara XML Toolkit -
http://www.xml.com/pub/a/2005/01/19/amara.html
Be humble, not imperial (in design) -
http://www.adtmag.com/article.asp?id=10286
UBL 1.0 -
http://www-106.ibm.com/developerworks/xml/library/x-think28.html
Manage XML collections with XAPI -
http://www-106.ibm.com/developerworks/xml/library/x-xapi.html
Default and error handling in XSLT lookup tables -
http://www.ibm.com/developerworks/xml/library/x-tiplook.html
Packaging XSLT lookup tables as EXSLT functions -
http://www.ibm.com/developerworks/xml/library/x-tiplook2.html





More information about the XML-SIG mailing list