473,473 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to Modify the XML segment using Python?

roberttonafelix
16 New Member
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
6 2796
bvdet
2,851 Recognized Expert Moderator Specialist
Do you want to change the text node value enclosed by a certain tag such as "QTY1"?
May 2 '13 #2
bvdet
2,851 Recognized Expert Moderator Specialist
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
16 New Member
thanks for the reply....

i need to change that <QTY> tag to <QTY1>,<QTY2>,<QTY3>....
May 3 '13 #4
bvdet
2,851 Recognized Expert Moderator Specialist
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
16 New Member
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
2,851 Recognized Expert Moderator Specialist
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

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

Similar topics

4
by: The_Incubator | last post by:
As the subject suggests, I am interested in using Python as a scripting language for a game that is primarily implemented in C++, and I am also interested in using generators in those scripts... ...
8
by: Sridhar R | last post by:
Hi, I am a little experienced python programmer (2 months). I am somewhat experienced in C/C++. I am planning (now in design stage) to write an IDE in python. The IDE will not be a simple...
8
by: Joakim Persson | last post by:
Hello all. I am involved in a project where we have a desire to improve our software testing tools, and I'm in charge of looking for solutions regarding the logging of our software (originating...
3
by: sumi | last post by:
How do i create a dir using python.........
29
by: 63q2o4i02 | last post by:
Hi, I'm interested in using python to start writing a CAD program for electrical design. I just got done reading Steven Rubin's book, I've used "real" EDA tools, and I have an MSEE, so I know what...
11
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure...
0
by: P. Adhia | last post by:
Hello, I was wondering if anyone is successfully using using Python(2.5)+DB2+pydb2. I get an error in all situations. It seems that this problem might be limited to python 2.5. A quick Google...
9
by: dominiquevalentine | last post by:
Hello, I'm a teen trying to do my part in improving the world, and me and my pal came up with some concepts to improve the transportation system. I have googled up and down for examples of using...
14
by: Nathan Pinno | last post by:
How do I factor a number? I mean how do I translate x! into proper Python code, so that it will always do the correct math? Thanks in advance, Nathan P.
53
by: Vicent Giner | last post by:
Hello. I am new to Python. It seems a very interesting language to me. Its simplicity is very attractive. However, it is usually said that Python is not a compiled but interpreted programming...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.