Home > front end >  beautifulsoup delete elements other than required element in xml tree
beautifulsoup delete elements other than required element in xml tree

Time:11-28

I am using beautifulsoup to delete an element from xml document. It is deleting required tag but also removing some other info from xml document which is not related to that element. How to stop this?

Code to reproduce:

import requests
from bs4 import BeautifulSoup

text_file = open('C:\Ashok\sample.xml', 'r')
s = text_file.read()

soup = BeautifulSoup(s, 'xml')
u = soup.find('Version', text='29.2.3')
fed = u.findParent()

fed.decompose()

f = open('C:\Ashok\sample.xml', "w")

f.write(str(soup))
f.close()

Find comparison attached. deleted other info showed in red rectangles.

enter image description here

It is updating Header and footer tags which I did not ask code to do.

CodePudding user response:

What happens?

The empty elements are not deleted only notation is transformed.

Empty elements in XML

An element with no content is empty and in XML, you can indicate an empty element like this:

<element></element>

An alternativ notation is the so called self-closing tag:

<element />

Both forms have identical results in XML readers, parsers,...

  • Related