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
#2
Could you provide the actual code you're running, so we can try it out? For example, I don't even know what module you're using, as find() or nodelist.remove() aren't part of either the DOM nor SAX interfaces.
Reply
#3
Below is the whole code that i'm using as of now.

 
import xml.etree.ElementTree as ET

tree = ET.parse("xml_test.txt")
root = tree.getroot()

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'))
The use case is that at some places in the sample XML, the <Value> tag is empty. So every-time we encounter a tag like this, we need to remove the corresponding <Attribute> tag from the xml itself.
Reply
#4
I believe that without using extra library like lxml etc., this requirement might not be possible, but as an implementation specialist i found a work-around for this.
You can replace your empty string with the 'NULL' keyword and then remove the line altogether where ever you find 'NULL' in the XML.
Reply


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