2

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()
Akash Rathor
  • 109
  • 8

1 Answers1

1

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>
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91