0

Hi everyone,

Does anyone know how to remove a particular attribute in XML by using Python?

<country>
<city capital="Paris">Paris</city>
............
............
</country>
<country>
<city capital="Helsinki">Helsinki</city>
............
............
</country>
<country>
<city capital="Bogota">Bogota</city>
............
............
</country>

I just wanna delete or remove Paris and Helsinki here by using SAX or DOM. How could I do it?

Thanks for your help.

Have a nice day!!!

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by bvdet
0

Using minidom:

from xml.dom import minidom

doc = '''<?xml version="1.0" ?>
<countries>
<country>
<city capital="Paris">Paris</city>
</country>
<country>
<city capital="Helsinki">Helsinki</city>
</country>
<country>
<city capital="Bogota">Bogota</city>
</country>
</countries>'''

docXML = minidom.parseString(doc)
for elem in docXML.getElementsByTagName("city"):
    elem.removeAttribute("capital")
print docXML.toprettyxml(indent="  ", newl="")
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.