Would you give me some advice how to modify element text in XML using python? if I want to insert other text in front of text of the first BBB element, which part should i change at the code below?
Please don't use fromstring and other modules(example lxml).
This is sample XML below.
<?xml version="1.0"?>
<data>
<AAA>
<CCC>
<BBB>This</BBB> ----> the first BBB element
</CCC>
<CCC>
<BBB>is</BBB>
</CCC>
<CCC>
<BBB>test1</BBB>
</CCC>
</AAA>
<AAA>
<CCC>
<BBB>This is test</BBB>
</CCC>
</AAA>
</data>
and it's code what i'm am trying below.
import xml.etree.ElementTree as ET
import re
tree = ET.parse("C:\\test\\python test\\data_text.xml")
root = tree.getroot()
for AAA in root.findall('AAA'):
for CCC in AAA.findall('CCC'):
for BBB in CCC.findall('BBB')[0]:
BBB_text = '11111' + BBB.text
print(BBB_text)
tree.write('C:\\test\\python test\\output.xml')
As far as i know, for BBB in CCC.findall('BBB')[0]:
[0] means find only the first BBB, but i guess it's wrong.
and this is the result that i want.
<?xml version="1.0"?>
<data>
<AAA>
<CCC>
<BBB>11111This</BBB> ----> the first BBB element
</CCC>
<CCC>
<BBB>is</BBB>
</CCC>
<CCC>
<BBB>test1</BBB>
</CCC>
</AAA>
<AAA>
<CCC>
<BBB>This is test</BBB>
</CCC>
</AAA>
</data>
BBB_text = '11111' + BBB.text. Shouldn't it beBBB.text = '11111' + BBB.text? Doesprint(BBB_text)prints the correct text ?