How to? Created a document and an element:
import xml.dom.minidom as d
a=d.Document()
b=a.createElement('test')
setIdAttribute doesn’t work 🙁
b.setIdAttribute('something')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/xml/dom/minidom.py", line 835, in setIdAttribute
self.setIdAttributeNode(idAttr)
File "/usr/lib/python2.6/xml/dom/minidom.py", line 843, in setIdAttributeNode
raise xml.dom.NotFoundErr()
xml.dom.NotFoundErr
And if I set this by hand, getElementById can’t find it.
b.setAttribute('id', 'something')
a.getElementById('something')
What I have to do?
Two things are wrong here.
Document.getElementByIdwill only find elements that are actually in the document. Here you’ve createdbbut not actually added it to the document. (It’s exactly the same in JavaScript.)You have to mark
idas an ID attribute usingsetIdAttribute. (There’s no need to do this in JavaScript because in HTML documents, attributes namedidare automatically considered to be ID attributes, logically enough. But XML does not automatically treat attributes namedidas IDs; you can either explicitly declare that they are in your DTD or callsetIdAttributeindividually for every ID attribute. And I am not sure the DTD thing will work with minidom, which is not a full DOM implementation.)Like so:
After that,
getElementByIdworks: