Akash Rathor
Akash Rathor

Reputation: 107

how to remove full element from xml using python and BeautifulSoup

for example I have a XML

<parent class="alpha">
<child>
<subchild>
</subchild>
</child>
</parent>
<parent class="beta">
<child>
<subchild>
</subchild>
</child>
</parent>
<parent class="gamma">
<child>
<subchild>
</subchild>
</child>
</parent>

I want to remove full parent element if class match in parent element. let say for example class="beta".

then I want the updated XML like this.

<parent class="alpha">
<child>
<subchild>
</subchild>
</child>
</parent>
<parent class="gamma">
<child>
<subchild>
</subchild>
</child>
</parent>

I tried , but not able to get desired results.

with open("path/to/xml","w") as fil2:
        Soup = soup.find_all("parent ",{'class':'beta'})
        for i in Soup:
                i.decompose()

Upvotes: 2

Views: 1098

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195553

To save the new file without specified tags, you can use this example:

from bs4 import BeautifulSoup


txt = '''<parent class="alpha">
<child>
<subchild>
</subchild>
</child>
</parent>
<parent class="beta">
<child>
<subchild>
</subchild>
</child>
</parent>
<parent class="gamma">
<child>
<subchild>
</subchild>
</child>
</parent>'''

soup = BeautifulSoup(txt, 'html.parser')

for p in soup.find_all("parent",{'class':'beta'}):
    p.decompose()

with open('new_file.xml', 'w') as f_out:
    print(soup, file=f_out)

Saves new_file.xml with content:

<parent class="alpha">
<child>
<subchild>
</subchild>
</child>
</parent>

<parent class="gamma">
<child>
<subchild>
</subchild>
</child>
</parent>

Upvotes: 1

Related Questions