Aug-20-2018, 04:04 PM
My use case is to remove empty tags in an XML using simple plain python 2.7. No extra lxml library is available.
Sample XML:
<ArML>
<MsgHeader>
<Deal>
<Attribute>
<Name>First</Name>
<Value>10</Value>
</Attribute>
<Attribute>
<Name>Second</Name>
<Value></Value>
</Attribute>
<Attribute>
<Name>Third</Name>
<Value></Value>
</Attribute>
<Attribute>
<Name>Fourth</Name>
<Value>40</Value>
</Attribute>
</Deal>
</MsgHeader>
<MsgHeader>
<Deal>
<Attribute>
<Name>Fifth</Name>
<Value>10</Value>
</Attribute>
<Attribute>
<Name>Sixth</Name>
<Value></Value>
</Attribute>
<Attribute>
<Name>Seventh</Name>
<Value>70</Value>
</Attribute>
<Attribute>
<Name>Eight</Name>
<Value></Value>
</Attribute>
</Deal>
</MsgHeader>
</ArML>
I am using the below code, but it is not working properly for all the empty tags. Please help.
Sample XML:
<ArML>
<MsgHeader>
<Deal>
<Attribute>
<Name>First</Name>
<Value>10</Value>
</Attribute>
<Attribute>
<Name>Second</Name>
<Value></Value>
</Attribute>
<Attribute>
<Name>Third</Name>
<Value></Value>
</Attribute>
<Attribute>
<Name>Fourth</Name>
<Value>40</Value>
</Attribute>
</Deal>
</MsgHeader>
<MsgHeader>
<Deal>
<Attribute>
<Name>Fifth</Name>
<Value>10</Value>
</Attribute>
<Attribute>
<Name>Sixth</Name>
<Value></Value>
</Attribute>
<Attribute>
<Name>Seventh</Name>
<Value>70</Value>
</Attribute>
<Attribute>
<Name>Eight</Name>
<Value></Value>
</Attribute>
</Deal>
</MsgHeader>
</ArML>
I am using the below code, but it is not working properly for all the empty tags. Please help.
for elem in root.iter('MsgHeader'): Deal = root.find("./MsgHeader/Deal") empty = root.find("./MsgHeader/Deal/Attribute/[Value='']") Deal.remove(empty) print(ET.tostring(root, encoding='utf8').decode('utf8'))