Python Forum
Remove Empty tags in XML using plain python without lxml library
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove Empty tags in XML using plain python without lxml library
#1
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.

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'))
Reply


Messages In This Thread
Remove Empty tags in XML using plain python without lxml library - by saurabhverma2412 - Aug-20-2018, 04:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python script that deletes symbols in plain text nzcan 3 718 Sep-05-2023, 04:03 PM
Last Post: deanhystad
  Remove empty keys in a python list python_student 7 3,112 Jan-12-2022, 10:23 PM
Last Post: python_student
  Parsing link from html tags with Python Melcu54 0 1,632 Jun-14-2021, 09:25 AM
Last Post: Melcu54
  Loop through tags inside tags in Selenium/Python xpack24 1 5,722 Oct-23-2019, 10:15 AM
Last Post: Larz60+
  How to remove empty struct from matlab file in python? python_newbie09 0 2,407 Jun-25-2019, 12:13 PM
Last Post: python_newbie09
  remove tags from BeautifulSoup result moski 1 4,719 Jun-05-2019, 01:47 PM
Last Post: heiner55
  lxml - etree/lxml need help storing variable for most inserted element goeb 0 2,583 Apr-01-2019, 03:09 AM
Last Post: goeb
  How to remove empty line between two print functions BigEasy 1 2,398 Feb-07-2018, 08:38 AM
Last Post: buran
  lxml saves empty tags with None text urano1981 0 4,707 Oct-23-2017, 11:45 AM
Last Post: urano1981
  PyInstaller, how to create library folder instead of library.zip file ? harun2525 2 4,863 May-06-2017, 11:29 AM
Last Post: harun2525

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020