Skip to main content
added 5 characters in body
Source Link
har07
  • 89.5k
  • 12
  • 86
  • 143

xmlRoot.findall('documentation') in your code didn't find anything, because <documentation> isn't direct child of the root element <data>. It is actually direct child of <config> :

"Element.findall() finds only elements with a tag which are direct children of the current element". [19.7.1.3. Finding interesting elements]

This is one possible way to remove all children of <config> using findall() given sample XML you posted (and assuming that the actual XML has <documentation> element closed with proper closing tag instead of closed with </script>) :

......
config = xmlRoot.find('config')

# find all children of config
for doc in config.findall('*'):
    config.remove(doc)
    # print just to make sure the element to be removed is correct
    print ElementTree.tostring(doc)
......

xmlRoot.findall('documentation') in your code didn't find anything, because <documentation> isn't direct child of the root element <data>. It is actually direct child of <config> :

"Element.findall() finds only elements with a tag which are direct children of the current element". [19.7.1.3. Finding interesting elements]

This is one possible way to remove all children of <config> using findall() given sample XML you posted (and assuming that the actual XML has <documentation> element closed with proper closing tag instead of closed with </script>) :

......
config = xmlRoot.find('config')

# find all children of config
for doc in config.findall('*'):
    config.remove(doc)
    # print to make sure the element to be removed is correct
    print ElementTree.tostring(doc)
......

xmlRoot.findall('documentation') in your code didn't find anything, because <documentation> isn't direct child of the root element <data>. It is actually direct child of <config> :

"Element.findall() finds only elements with a tag which are direct children of the current element". [19.7.1.3. Finding interesting elements]

This is one possible way to remove all children of <config> using findall() given sample XML you posted (and assuming that the actual XML has <documentation> element closed with proper closing tag instead of closed with </script>) :

......
config = xmlRoot.find('config')

# find all children of config
for doc in config.findall('*'):
    config.remove(doc)
    # print just to make sure the element to be removed is correct
    print ElementTree.tostring(doc)
......
Source Link
har07
  • 89.5k
  • 12
  • 86
  • 143

xmlRoot.findall('documentation') in your code didn't find anything, because <documentation> isn't direct child of the root element <data>. It is actually direct child of <config> :

"Element.findall() finds only elements with a tag which are direct children of the current element". [19.7.1.3. Finding interesting elements]

This is one possible way to remove all children of <config> using findall() given sample XML you posted (and assuming that the actual XML has <documentation> element closed with proper closing tag instead of closed with </script>) :

......
config = xmlRoot.find('config')

# find all children of config
for doc in config.findall('*'):
    config.remove(doc)
    # print to make sure the element to be removed is correct
    print ElementTree.tostring(doc)
......