'Trouble setting a value using XML Etree
I am trying to change the value of a node from an XML file.
I am using XML Etree/Element Tree.
My code is the following:
import xml.etree.ElementTree as ET
import array
tree = ET.parse('Beckhoff ELM37xx.xml')
root = tree.getroot()
global xdevice
for Descriptions in root.findall('.//Descriptions'):
xdescriptions = Descriptions.find('Devices')
for Devices in root.findall('.//Devices'):
xdevices = Devices.find('Device')
for Device in root.findall('.//Device'):
xdevice = Device.find('Name').text
print(xdevice)
##tree.write('Beckhoff ELM37xx.xml')
With xdevice
, I am getting the information I need displayed, it's 10 elements.
ELM3702-0000 2Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision
ELM3702-0000 2Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision
ELM3704-0000 4Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision
ELM3704-0000 4Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision
ELM3704-0001 4Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision, LEMO
ELM3704-0020 4Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision, calibrated
ELM3704-0020 4Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision, calibrated
ELM3702-0101 2Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision, isolated channels
ELM3704-1001 4Ch. Ana. Input +/-10V, TC, 24 bit, high precision
ELM3704-1001 4Ch. Ana. Input +/-10V, TC, 24 bit, high precision
I would like to understand how to change the first value using set
, since indexing doesn't work, it gives back the letter E for each iteration. I want to understand how to store this values into a list or a tuple, in order to be able to modify it (or modifying it directly but just one value of the list at will).
Solution 1:[1]
It got solved:
import xml.etree.ElementTree as ET
tree = ET.parse('Beckhoff ELM37xx.xml')
root = tree.getroot()
Devices = root.findall('.//Device')
nametag1 = Devices[0].find('Name')
nametag1.text = 'Blanqui'
for Device in root.findall('.//Device'):
xdevice = Device.find('Name').text
print(xdevice)
tree.write('Beckhoff ELM37xx.xml')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Blanqui |