On 1 Nov 2005 11:09:10 -0800, PyPK <[EMAIL PROTECTED]> wrote:
How do I add a new attribute to the existing xml Document tree???

Add an attribute of an element, or add a new element?

I hope I've understood your question correctly...this demonstrates both adding a brand new element (tag) and an attribute for it.

from xml.dom.ext.reader.Sax2 import FromXmlStream

doc = FromXmlStream(stream)  # or FromXmlStream(open(name_of_file))
NewElement = doc.createElement('NewElement')
NewElement.setAttribute('attribute_name', 'attribute_value')
doc.documentElement.appendChild(NewElement)

You'll wind up with <NewElement attribute_name='attribute_value' />
You may need to use getElementsByTagName or other means to find the exact node to place your new element if you aren't putting it directly under the root element.

To save into the original xml file, I create a filehandle to the file, then PrettyPrint(doc, fh).

DISLAIMER: I just learned this last week, so if anything is wrong or could be improved, the list is welcome to correct me ;)

Rob
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to