[XML-SIG] Re: Could somebody help me?
Prasad PS
prasad_st at beceem.com
Fri Jan 28 11:18:09 CET 2005
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 for each appending of node, the file is opened and the new root
document with the appeneded node is re-written to the file. This is
taking lot of time.
Is there any way to append a node to the xml file without re-writing the
file?
Prasad.p.s.
fp = open(
string.strip(self.cnfDtls.GetLogFilePath()), 'r')
doc = FromXmlStream(fp)
## else:
## print "File not available"
## print "The Doc is ", doc
#### self.testcases.appendChild(doc.createTextNode("\n "))
## print
**************************************************************", doc
top_nodeList = doc.getElementsByTagName("Logger")
## print "Hi", top_nodeList
tc = doc.createElement("LogDetails")
## top_nodeList[0].appendChild(tc)
tc.appendChild(doc.createTextNode("\n "))
i=0
for elements in self.firstPart:
ln = doc.createElement(elements)
if elements == "Steps":
stepParts = self.parts[i]
## print "The Step Part is ", stepParts
for index in range(len(stepParts)):
step = stepParts[index]
msgSent = step[0]
msgRecd = step[1]
timeout = step[2]
stateMsg = step[3]
stepNode = doc.createElement("Step")
mvalueNode1 = doc.createElement("MessageSent")
for msgIndex in range(len(msgSent)):
##
stepNode.appendChild(mvalueNode1)
keyL = msgSent[msgIndex].keys()
for m in range(len(keyL)):
keyNode1 = doc.createElement(keyL[m])
valL = msgSent[msgIndex][keyL[m]]
if type(valL) == str:
## attNode0 = doc.createElement()
keyNode1.appendChild(doc.createTextNode(valL))
## mvalueNode1.appendChild(keyNode1)
else:
keysL = valL.keys()
for n in range(len(keysL)):
attNode1 = doc.createElement(keysL[n])
attNode1.appendChild(doc.createTextNode(valL[keysL[n]]))
keyNode1.appendChild(attNode1)
mvalueNode1.appendChild(keyNode1)
stepNode.appendChild(mvalueNode1)
mvalueNode2 = doc.createElement("MessageReceived")
for msgIndex in range(len(msgRecd)):
keyL = msgRecd[msgIndex].keys()
for m in range(len(keyL)):
keyNode2 = doc.createElement(keyL[m])
valL2 = msgRecd[msgIndex][keyL[m]]
if type(valL2) == str:
keyNode2.appendChild(doc.createTextNode(valL2))
## mvalueNode2.appendChild(keyNode1)
else:
keysL = valL2.keys()
for n in range(len(keysL)):
attNode2 = doc.createElement(keysL[n])
attNode2.appendChild(doc.createTextNode(valL2[keysL[n]]))
keyNode2.appendChild(attNode2)
mvalueNode2.appendChild(keyNode2)
stepNode.appendChild(mvalueNode2)
timeoutNode = doc.createElement("TimeOut")
timeoutNode.appendChild(doc.createTextNode(timeout))
stepNode.appendChild(timeoutNode)
stateMsgNode = doc.createElement("StateMessage")
stateMsgNode.appendChild(doc.createTextNode(stateMsg))
stepNode.appendChild(stateMsgNode)
ln.appendChild(stepNode)
else:
ln.appendChild(doc.createTextNode(self.parts[i]))
tc.appendChild(ln)
tc.appendChild(doc.createTextNode("\n "))
i = i+1
t = doc.createTextNode("\n")
top_nodeList[0].appendChild(tc)
top_nodeList[0].appendChild(t)
fp.close()
fp = open (string.strip(self.cnfDtls.GetLogFilePath()), 'w')
xml.dom.ext.PrettyPrint(doc, self.xmlFile)
self.xmlFile.write("\n")
fp.close()
-----Original Message-----
From: xml-sig-bounces at python.org [mailto:xml-sig-bounces at python.org] On
Behalf Of Fredrik Lundh
Sent: Friday, January 28, 2005 3:34 PM
To: xml-sig at python.org
Subject: [XML-SIG] Re: Could somebody help me?
Prasad PS wrote:
> I too have followed the second choice but what happened was, when I
add
> the root document to the xml file, I find the previous content and the
< combination of the previous and the new content in the file and
moreover
> xml declarator is appearing twice.
can you perhaps post a short snippet that illustrates the problem?
is using a traditional DOM API an absolute requirement, btw?
</F>
_______________________________________________
XML-SIG maillist - XML-SIG at python.org
http://mail.python.org/mailman/listinfo/xml-sig
More information about the XML-SIG
mailing list