Need help to change values in XML using Python? noob help pls!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to change values in XML using Python? noob help pls!
# 1  
Old 08-14-2011
Need help to change values in XML using Python? noob help pls!

Hello I am a noob in XML and Python. I am trying to do this for my MSc project about a network simulation and need some help.... I want to change the values shown below:

Code:
<num_crash_failures>1</num_crash_failures>
−<crash_failure_entry>
<freeze_at_slot>0</freeze_at_slot>
<freeze_duration>0</freeze_duration>
<freeze_num>0</freeze_num>
<freeze_repeat_rate>0</freeze_repeat_rate>
</crash_failure_entry>

The code goes like this. Basically there are some parameters for every node in the system and the nodes goes like node0 node1 and every parameter name is same. I managed to change a single value like this:

Code:
# create a backup of original file
new_file_name = 'temp/1_cluster_TTP.dat.xml'
old_file_name = new_file_name + "backup"
os.rename(new_file_name, old_file_name)

# change text value of element
doc = parse(old_file_name)
node = doc.getElementsByTagName('num_clusters')
node[0].firstChild.nodeValue = '1'

# persist changes to new file
xml_file = open(new_file_name, "w")
doc.writexml(xml_file, encoding="utf-8")
xml_file.close()

But the problem is "num_clusters" is exists in only one place. The real parameters I want to change is in every nodes' parameter and the names are also the same. How can I modify this code to reach and change every values that I mentioned above? Any help is appreciated. Thank you.


Code:
<Node>
<Name>Node_0</Name>
<coldstart_flag>1</coldstart_flag>
<max_coldstart_frames>1</max_coldstart_frames>
<CIA>1</CIA>
<time_master_node>0</time_master_node>
<gateway_connection>0</gateway_connection>
<sys_drift>-0.000250</sys_drift>
<stoch_drift>0.000000</stoch_drift>
<logging_disabled>0</logging_disabled>
−
<Failures>
<crash_failures_enabled>1</crash_failures_enabled>
−
<Crash>
<num_crash_failures>1</num_crash_failures>
−
<crash_failure_entry>
<freeze_at_slot>0</freeze_at_slot>
<freeze_duration>0</freeze_duration>
<freeze_num>0</freeze_num>
<freeze_repeat_rate>0</freeze_repeat_rate>
</crash_failure_entry>
</Crash>
<transmission_failures_enabled>0</transmission_failures_enabled>
−
<Transmission>
<num_transmission_failures>1</num_transmission_failures>
−
<transmission_failure_entry>
<faulty_msg_in_round>0</faulty_msg_in_round>
<faulty_msg_num>0</faulty_msg_num>
<faulty_msg_repeat_rate>0</faulty_msg_repeat_rate>
</transmission_failure_entry>
</Transmission>
<clock_state_failures_enabled>0</clock_state_failures_enabled>
−
<Clock_state>
<num_clock_state_failures>1</num_clock_state_failures>
−
<clock_state_failure_entry>
<change_clock_state_at_slot>0</change_clock_state_at_slot>
<change_clock_state_ticks>0</change_clock_state_ticks>
<change_clock_state_num>0</change_clock_state_num>
<change_clock_state_repeat_rate>0</change_clock_state_repeat_rate>
</clock_state_failure_entry>
</Clock_state>
<clock_rate_failures_enabled>0</clock_rate_failures_enabled>
−
<Clock_rate>
<num_clock_rate_failures>1</num_clock_rate_failures>
−
<clock_rate_failure_entry>
<change_drift_at_slot>0</change_drift_at_slot>
<change_drift_by>0.000000</change_drift_by>
<change_drift_num>0</change_drift_num>
<change_drift_repeat_rate>0</change_drift_repeat_rate>
</clock_rate_failure_entry>
</Clock_rate>
</Failures>
</Node>
−
<Node>
<Name>Node_1</Name>
<coldstart_flag>0</coldstart_flag>
<max_coldstart_frames>1</max_coldstart_frames>
<CIA>1</CIA>
<time_master_node>0</time_master_node>
<gateway_connection>0</gateway_connection>
<sys_drift>-0.000150</sys_drift>
<stoch_drift>0.000000</stoch_drift>
<logging_disabled>0</logging_disabled>
−
<Failures>
<crash_failures_enabled>0</crash_failures_enabled>
−
<Crash>
<num_crash_failures>1</num_crash_failures>
−
<crash_failure_entry>
<freeze_at_slot>0</freeze_at_slot>
<freeze_duration>0</freeze_duration>
<freeze_num>0</freeze_num>
<freeze_repeat_rate>0</freeze_repeat_rate>
</crash_failure_entry>
</Crash>
<transmission_failures_enabled>0</transmission_failures_enabled>
−
<Transmission>
<num_transmission_failures>1</num_transmission_failures>
−
<transmission_failure_entry>
<faulty_msg_in_round>0</faulty_msg_in_round>
<faulty_msg_num>0</faulty_msg_num>
<faulty_msg_repeat_rate>0</faulty_msg_repeat_rate>
</transmission_failure_entry>
</Transmission>
<clock_state_failures_enabled>0</clock_state_failures_enabled>
−
<Clock_state>
<num_clock_state_failures>1</num_clock_state_failures>
−
<clock_state_failure_entry>
<change_clock_state_at_slot>0</change_clock_state_at_slot>
<change_clock_state_ticks>0</change_clock_state_ticks>
<change_clock_state_num>0</change_clock_state_num>
<change_clock_state_repeat_rate>0</change_clock_state_repeat_rate>
</clock_state_failure_entry>
</Clock_state>
<clock_rate_failures_enabled>0</clock_rate_failures_enabled>
−
<Clock_rate>
<num_clock_rate_failures>1</num_clock_rate_failures>
−
<clock_rate_failure_entry>
<change_drift_at_slot>0</change_drift_at_slot>
<change_drift_by>0.000000</change_drift_by>
<change_drift_num>0</change_drift_num>
<change_drift_repeat_rate>0</change_drift_repeat_rate>
</clock_rate_failure_entry>
</Clock_rate>
</Failures>


Last edited by pludi; 08-14-2011 at 08:29 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Only print specific xml values that meet two criteria in python

I have a large XML file that I want to parse, and only print one specific value if two values are met. This is the code so far: #!/usr/local/bin/python import xml.etree.ElementTree as ET tree = ET.parse('onedb-dhcp.xml') root = tree.getroot() # This successfully gets all... (1 Reply)
Discussion started by: brianjb
1 Replies

2. UNIX for Beginners Questions & Answers

How to change values in xml file?

I have xml file like below, i want change the values at default-value place of each argument name using shell script. like where argument name= protocol and default-value=tcp, where argument name =port and default-value= 7223, where argument name = username and default-value=test, example ... (12 Replies)
Discussion started by: s1s2s3s4
12 Replies

3. Programming

Python noob menu

Hey guys, literally just starting off on python, just making a simple menu based system to read/write files and delete a file to get used to calling functions and working with the file system etc. For some reason the code won't write to the file I have on my system, not sure what I'm doing wrong.... (3 Replies)
Discussion started by: 3therk1ll
3 Replies

4. Shell Programming and Scripting

ksh CSV to XML file (noob tier)

Hey all, I'm very new to shell scripting and would love some help. I have been messing around with KSH at my job, and have been tasked with generating an XML file from multiple CSV files. However, I barely even understand the syntax for for loops! Output should be something along the lines of ... (2 Replies)
Discussion started by: Parrakarry
2 Replies

5. Shell Programming and Scripting

Need help to change XML values with shell scripting for Network Simulation

Hello, I don't have experience in this scripting and I need some help to read a value from an XML file and change it with a random number to use in simulator for different network scenarios. </Description><sim_comm_rounds>35</sim_comm_rounds><num_clusters>1</num_clusters><Clocking> I want to... (5 Replies)
Discussion started by: erhanasd
5 Replies

6. Shell Programming and Scripting

Change values in Log4j.xml using ksh script

Hi, I am new to UNIX and shell scripting. I have to create a shell script(ksh) which parses log4j.xml file for a given webservice name and change the corresponding value from INFO to DEBUG or vice-versa. My log4j.xml looks like:- <!-- Appender WEBSERVICENAME--> <appender... (3 Replies)
Discussion started by: sanjeevcseng
3 Replies

7. Programming

Needing help parsing XML/RDF using Python

Hello, I am trying to make script to parse the install.rdf files found in firefox xpi extentions to isolate the extention ID so I can name a directory and automate installation of system-wide extension. I am very facile with the command line, but not with programming languages (esp... (0 Replies)
Discussion started by: Narnie
0 Replies

8. Shell Programming and Scripting

Help Reading XML files in Python: Urgent

OK so my objective is to create a python program that will parse an XML file(input.xml), then the program will create an mxml(output.mxml) file. In the program (.py) i need to read between CDATA so that I would get an output the CDATA code in the input.xml INPUT.XML <!]> ... (2 Replies)
Discussion started by: dcfivesixfive
2 Replies

9. Post Here to Contact Site Administrators and Moderators

Pls change my userid

Can you change my userid from "jdmower" to "jdmowrer". I dropped an r when registering. thanks Jim (0 Replies)
Discussion started by: jdmowrer
0 Replies

10. Shell Programming and Scripting

To Change the file format Pls Help!!!

Hi All, I have a file like john::208:johnson john::208:mery john::208:test admin:*:1:johnson admin:*:1:test and wanna convert this as john::208:johnson,mery,test admin:*:1:johnson,test please help me to create a script for this thanks in advance John (4 Replies)
Discussion started by: johnsonpk
4 Replies
Login or Register to Ask a Question