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>$>ñô«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>$>ñô«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
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"?
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". - def setTextElem(self, parent, text):
-
'''Set the first child text element data of parent element. Parent
-
and child must be of the form "<head_diam>1.375</head_diam>".'''
-
success = False
-
for n in parent.childNodes:
-
# TEXT_NODE - 3
-
if n.nodeType == 3:
-
n.data = text
-
success = True
-
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.
thanks for the reply....
i need to change that <QTY> tag to <QTY1>,<QTY2>,<QTY3>....
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. - >>> a = Parser("test.xml")
-
>>> print a.backToXML()
-
<?xml version="1.0" ?>
-
<person_details>
-
<person>
-
<name>Mary</name>
-
<location> loc1 </location>
-
</person>
-
</person_details>
-
>>> n = a.doc.getElementsByTagName("name")[0]
-
>>> n1 = a.doc.getElementsByTagName("person")[0]
-
>>> new = a.doc.createElement("name1")
-
>>> new.appendChild(a.doc.createTextNode("XYZ"))
-
<DOM Text node "'XYZ'">
-
>>> n1.replaceChild(new, n)
-
<DOM Element: name at 0x610c2c8>
-
>>> print a.backToXML()
-
<?xml version="1.0" ?>
-
<person_details>
-
<person>
-
<name1>XYZ</name1>
-
<location> loc1 </location>
-
</person>
-
</person_details>
-
>>>
thanks for the reply dude....
but i complete myself using replace....
here is the code which i created.... - import os, glob
-
import time
-
from datetime import datetime
-
now=datetime.now()
-
filepath = "*****************"
-
os.chdir(filepath)
-
for files in glob.glob("*.*"):
-
r0=now.strftime("%Y/%m/%d-%H:%M:%S")
-
curdate=now.strftime("%d-%m-%Y")
-
filename=filepath+files
-
file1 = open(filename,'r')
-
file3=str(file1.read())
-
file2 = open("*********"+files,'w')
-
string0=file3.replace('<ZACGPIAD SEGMENT="1">','<ZACGPIADD SEGMENT="1">')
-
string1=string0.replace('</ZACGPIAD>','</ZACGPIADD>')
-
string2=string1.replace('<QTY>','<QTY1>', 1)
-
string3=string2.replace('</QTY>','</QTY1>', 1)
-
string4=string3.replace('<QTY>','<QTY2>', 1)
-
string5=string4.replace('</QTY>','</QTY2>', 1)
-
string6=string5.replace('<QTY>','<QTY3>', 1)
-
string7=string6.replace('</QTY>','</QTY3>', 1)
-
file2.write(string7)
-
print files + " Done"
-
file2.close()
-
filename1=curdate+"_"+"invoicefiles.txt"
-
log=file("D:\\Invoice_transfer\\Log\\"+filename1,"a+")
-
print >> log,"File name: "+ filename +" Processed time: "+r0
-
log.close()
-
else:
-
print "No files found"
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
...
|
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...
|
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...
|
by: sumi |
last post by:
How do i create a dir using python.........
|
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...
| |
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...
|
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...
|
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...
|
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.
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |