I would like to change/add a custom subelement to an xml which was generated by my script.
The top element is AAA:
top = Element('AAA')
The collected_lines looks like this:
[['TY', ' RPRT'], ['A1', ' Peter'], ['T3', ' Something'], ['ER', ' ']]
Then I enumerate all lines one-by-one and create a SubElement for top
:
for line in enumerate(collected_lines):
child = SubElement(top, line[0])
child.text = line[1]
Output:
<?xml version="1.0" ?>
<AAA>
<TY> RPRT</TY>
<A1> Peter</A1>
<T3> Something</T3>
<ER> </ER>
</AAA>
And I would like to add <ART>
element to the top
element and then print the xml like this:
<?xml version="1.0" ?>
<AAA>
<ART>
<TY> RPRT</TY>
<A1> Peter</A1>
<T3> Something</T3>
<ER> </ER>
</ART>
</AAA>
I'm tried to do it with an if statemant. Like:
if "TY" in line:
"append somehow before TY element, <ART>"
if "ER" in line:
"append somehow after ER element, </ART>"
Is there a simple way to solve this?
No comments:
Post a Comment