# -*- coding: utf-8 -*- import xml.etree.ElementTree as ET ET.register_namespace("nms", "http://www.mycompany.com") tree = ET.parse(r"test.xml") root = tree.getroot() print len(root) id = 1 for document in root: print document.get('id') document.set('id', "%d"%(id)) id = id + 1 tree.write(r"test.xml", encoding='utf-8', xml_declaration=True)
test.xml:
<?xml version='1.0' encoding='utf-8'?> <nms:docset xmlns:nms="http://www.mycompany.com"> <nms:document id="1"> <content>text<>!@#$%^&*()_-=+\/{}[] context haha</content> </nms:document> <nms:document id="2"> <content>text<>!@#$%^&*()_-=+\/{}[] context haha</content> </nms:document> <nms:document id="3"> <content>text<>!@#$%^&*()_-=+\/{}[] context haha</content> </nms:document> <nms:document id="4"> <content>text<>!@#$%^&*()_-=+\/{}[] context haha</content> </nms:document> <nms:document id="5"> <content>text<>!@#$%^&*()_-=+\/{}[] context haha</content> </nms:document> <nms:document id="6"> <content>text<>!@#$%^&*()_-=+\/{}[] context haha</content> </nms:document> </nms:docset>
http://stackoverflow.com/questions/13372604/python-elementtree-parsing-unbound-prefix-error
http://stackoverflow.com/questions/4997848/emitting-namespace-specifications-with-elementtree-in-python