Home > Software design >  xml tree remove elements
xml tree remove elements

Time:04-05

I am trying to remove elements from my tree based on the elements attributes.

import xml.etree.cElementTree as ET
import requests

xml = requests.get("https://iptv-org.github.io/epg/guides/ca/sportsnet.ca.epg.xml").text
tree = ET.ElementTree(ET.fromstring(xml))
root = tree.getroot()

for elem in tree.iter():
    if elem.tag == "channel" or elem.tag == "programme":
        if elem.attrib.get("id", "") == "WWENetworkCanada.us" or elem.attrib.get("channel", "") == "WWENetworkCanada.us":
            pass
        else:
            print("removing")
            root.remove(elem)

ET.dump(root)

The dump still has the elements I am trying to remove, even though I see "removing". Can anyone see why?

I am expecting everything that is not "WWENetworkCanada.us" to be removed.

Thanks, Chris

CodePudding user response:

It was failing because I was changing indexes as I iterating and removing. What I needed to do was for elem in list(tree.iter()):. This is something I learnt some time ago (on here) that lets you remove while iterating without creating a "temp" list.

CodePudding user response:

Saw you answered your own question, but thought I'd share as an alternate approach. Simply use findall() to find and remove any values that don't match what you are looking for

Remove Unwanted Elements using findall()

for elem in tree.findall("./channel/[@id!='WWENetworkCanada.us']"):
    root.remove(elem)
    
for elem in tree.findall("./programme/[@channel!='WWENetworkCanada.us']"):
    root.remove(elem)
  • Related