1

How can we delete tags with particular Element name in an xml code using xml.dom (not lxml.etree) in python.

e.g. if we have the following xml code:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
   <author>Gambardella, Matthew</author>
   <title>XML Developer's Guide</title>
   <genre>Computer</genre>
   <price>44.95</price>
   <publish_date>2000-10-01</publish_date>
   <description>An in-depth look at creating applications 
   with XML.</description>
   </book>
   <book id="bk102">
   <author>Ralls, Kim</author>
   <title>Midnight Rain</title>
   <genre>Fantasy</genre>
   <price>5.95</price>
   <publish_date>2000-12-16</publish_date>
   <description>A former architect battles corporate zombies, 
   an evil sorceress, and her own childhood to become queen 
   of the world.</description>
   </book>
   <book id="bk103">
  <author>Corets, Eva</author>
  <title>Maeve Ascendant</title>
  <genre>Fantasy</genre>
  <price>5.95</price>
  <publish_date>2000-11-17</publish_date>
  <description>After the collapse of a nanotechnology 
  society in England, the young survivors lay the 
  foundation for a new society.</description>
  </book>
  <book id="bk104">
  <author>Corets, Eva</author>
  <title>Oberon's Legacy</title>
  <genre>Fantasy</genre>
  <price>5.95</price>
  <publish_date>2001-03-10</publish_date>
  <description>In post-apocalypse England, the mysterious 
  agent known only as Oberon helps to create a new life 
  for the inhabitants of London. Sequel to Maeve 
  Ascendant.</description>
  </book>
</catalog>

How can we delete all the tags named <title> using xml.dom in python?

1 Answer 1

6

Select all the elements by title tag and remove them using removeChild

Update:

from xml.dom import minidom
xmldoc = minidom.parse("book_xml.xml")

nodes = xmldoc.getElementsByTagName("title")

for node in nodes:
    parent = node.parentNode
    parent.removeChild(node) 

print xmldoc.toxml()

You can also write new changes to xml file by :

with open("result.xml","w") as fs:
    fs.write(xmldoc.toxml())
    fs.close()
Sign up to request clarification or add additional context in comments.

3 Comments

can you please explain how to use 'removeChild' here? Your code would just print the tags that I want to delete. Thanks.
Thank You so much. It(Update) works perfectly.(For future readers : 'doc_root' here is not required as we are not making use of it anywhere). Many thanks again.
see the updated code doc_root removed, file write also added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.