0

I've used xml.etree.ElementTree and can parse through a L5XX with no problem but when I go to make a change to it, it breaks it.

</Rung> <Rung Number="0" Type="N"> <Text>" <![CDATA[[XIC(start);]]> </Text>

It changes the text but deletes the CDATA at the beginning of the line of code,

</Rung> <Rung Number="0" Type="N"> <Text>XIC(start);< /></Text>

Is it just the library formatting it incorrectly and I need to do it another way, or am I doing something wrong?

Code will be below.

import xml.etree.ElementTree as ET

xmlfile = (r"C:\Users\burgeB9\Desktop\L5X_Editor\MES Core
Build\_002_IGNITION_DATA_OVERALL_MACHINE.L5X")       
tree = ET.parse(xmlfile)
RSLogix5000Content = tree.getroot()

#ET.dump(tree)

# Select Rung 0
Rung0 = RSLogix5000Content.find(".//Rung[@Number= '0']/Text")
print(Rung0.tag, Rung0.attrib, Rung0.text)

# Change Value in Rung 0 
for Rung in RSLogix5000Content.findall(".//Rung[@Number= '0']/Text"):
    Rung.text = "XIC(START);"
    print(Rung.tag, Rung.attrib, Rung.text)

"""tree.write(xmlfile[:-4]+ "edit_XML")"""
mzjn
  • 48,958
  • 13
  • 128
  • 248
  • ElementTree does not preserve CDATA sections. See (for example) https://stackoverflow.com/q/174890/407651 and https://stackoverflow.com/q/55217892/407651 – mzjn May 18 '23 at 17:07
  • Does this answer your question? [How to output CDATA using ElementTree](https://stackoverflow.com/questions/174890/how-to-output-cdata-using-elementtree) – Ben Miller - Remember Monica May 18 '23 at 17:34

0 Answers0