By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,673 Members | 1,310 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,673 IT Pros & Developers. It's quick & easy.

How to Modify the XML segment using Python?

roberttonafelix
P: 16
Hi Friends,

i have an xml file which has some data as below...

<ZACGPIAD SEGMENT="1">
<IDENTIFIER>D000</IDENTIFIER>
<CUST_DEL_NO/>
<CUST_DEL_DATE/>
<TRUCKNO/>
<DRIVERNAME/>
<DRIVERID/>
<RESPONS_OFF/>
<CONFIRM_DATE>$&gt;xr</CONFIRM_DATE>
<SERIAL_NO>9</SERIAL_NO>
<SERIAL_CHAR/>
<DEL_INFO1/>
<QTY>0</QTY>
<DEL_INFO2/>
<QTY>0</QTY>
<DEL_INFO3/>
<QTY>0</QTY>
<TRANS_COMPANY>0</TRANS_COMPANY>
</ZACGPIAD>

this segment is need to be changed as shown below...

<ZACGPIADD SEGMENT="1">
<IDENTIFIER>D000</IDENTIFIER>
<CUST_DEL_NO/>
<CUST_DEL_DATE/>
<TRUCKNO/>
<DRIVERNAME/>
<DRIVERID/>
<RESPONS_OFF/>
<CONFIRM_DATE>$&gt;xr</CONFIRM_DATE>
<SERIAL_NO>9</SERIAL_NO>
<SERIAL_CHAR/>
<DEL_INFO1/>
<QTY1>0</QTY1>
<DEL_INFO2/>
<QTY2>0</QTY2>
<DEL_INFO3/>
<QTY3>0</QTY3>
<TRANS_COMPANY>0</TRANS_COMPANY>
</ZACGPIADD>

i can able to change the segment name but that <QTY> tag i dont have any idea that how to make this using python...

Kindly guide me if it is possible to do...

Thanks in advance,

Robert.J
May 2 '13 #1
Share this Question
Share on Google+
6 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
Do you want to change the text node value enclosed by a certain tag such as "QTY1"?
May 2 '13 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Assuming your answer to my question is in the affirmative, given a parent node "parent", change the following text node to the value "text".
Expand|Select|Wrap|Line Numbers
  1.     def setTextElem(self, parent, text):
  2.         '''Set the first child text element data of parent element. Parent
  3.         and child must be of the form "<head_diam>1.375</head_diam>".'''
  4.         success = False
  5.         for n in parent.childNodes:
  6.             # TEXT_NODE - 3
  7.             if n.nodeType == 3:
  8.                 n.data = text
  9.                 success = True
  10.         return success
The above is a method of an XML parser class. This only changes the document object in memory - you will need to write the document to disc to make the change permanent.
May 2 '13 #3

roberttonafelix
P: 16
thanks for the reply....

i need to change that <QTY> tag to <QTY1>,<QTY2>,<QTY3>....
May 3 '13 #4

bvdet
Expert Mod 2.5K+
P: 2,851
That's a bit more involved. I'm not an XML expert, so there may be a better way. Create a new node element with the name you want and child text node. Get the parent node and the node you want to replace and apply xml doc method replaceChild. Following is an example:

Parser is an xml parser class.
Expand|Select|Wrap|Line Numbers
  1. >>> a = Parser("test.xml")
  2. >>> print a.backToXML()
  3. <?xml version="1.0" ?>
  4. <person_details>
  5.   <person>
  6.     <name>Mary</name>
  7.     <location> loc1 </location>
  8.   </person>
  9. </person_details>
  10. >>> n = a.doc.getElementsByTagName("name")[0]
  11. >>> n1 = a.doc.getElementsByTagName("person")[0]
  12. >>> new = a.doc.createElement("name1")
  13. >>> new.appendChild(a.doc.createTextNode("XYZ"))
  14. <DOM Text node "'XYZ'">
  15. >>> n1.replaceChild(new, n)
  16. <DOM Element: name at 0x610c2c8>
  17. >>> print a.backToXML()
  18. <?xml version="1.0" ?>
  19. <person_details>
  20.   <person>
  21.     <name1>XYZ</name1>
  22.     <location> loc1 </location>
  23.   </person>
  24. </person_details>
  25. >>> 
May 3 '13 #5

roberttonafelix
P: 16
thanks for the reply dude....

but i complete myself using replace....

here is the code which i created....

Expand|Select|Wrap|Line Numbers
  1. import os, glob
  2. import time
  3. from datetime import datetime
  4. now=datetime.now()
  5. filepath = "*****************"
  6. os.chdir(filepath)
  7. for files in glob.glob("*.*"):
  8.     r0=now.strftime("%Y/%m/%d-%H:%M:%S")
  9.     curdate=now.strftime("%d-%m-%Y")
  10.     filename=filepath+files
  11.     file1 = open(filename,'r')
  12.     file3=str(file1.read())
  13.     file2 = open("*********"+files,'w')
  14.     string0=file3.replace('<ZACGPIAD SEGMENT="1">','<ZACGPIADD SEGMENT="1">')
  15.     string1=string0.replace('</ZACGPIAD>','</ZACGPIADD>')
  16.     string2=string1.replace('<QTY>','<QTY1>', 1)
  17.     string3=string2.replace('</QTY>','</QTY1>', 1)
  18.     string4=string3.replace('<QTY>','<QTY2>', 1)
  19.     string5=string4.replace('</QTY>','</QTY2>', 1)
  20.     string6=string5.replace('<QTY>','<QTY3>', 1)
  21.     string7=string6.replace('</QTY>','</QTY3>', 1)
  22.     file2.write(string7)
  23.     print files + " Done"
  24.     file2.close()
  25.     filename1=curdate+"_"+"invoicefiles.txt"
  26.     log=file("D:\\Invoice_transfer\\Log\\"+filename1,"a+")
  27.     print >> log,"File name: "+ filename +" Processed time: "+r0
  28.     log.close()
  29. else:
  30.     print "No files found"
May 4 '13 #6

bvdet
Expert Mod 2.5K+
P: 2,851
roberttonafelix,

Whatever works for you! Working with text files, str replace would typically be the way to do it. I would prefer an XML parse solution when working with XML though.
May 4 '13 #7

Post your reply

Sign in to post your reply or Sign up for a free account.