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
#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


Messages In This Thread
RE: Remove Empty tags in XML using plain python without lxml library - by saurabhverma2412 - Aug-20-2018, 05:08 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,862 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