|
[howto]create and add element in minidom: msg#00008python.xml
http://www.lucasmanual.com/mywiki/ Would you know of similar easy howto for using xpath(element reference), and another one for traversing xml docs? thx Lucas Create and add elements from xml.dom.minidom import parseString * creating new document, and root element at the same time. doc = parseString(u'<top/>'.encode('UTF-8')) * print doc.toprettyxml() will show you how it looks * this would create: <?xml version="1.0" ?> <top/> * Now we reference to our <top/> element by: top_element=doc.documentElement * We create another element by: element1=doc.createElementNS(None,u'section1') * Add it under our top element by: top_element.appendChild(element1) * Create another element and add it under element1 element1.appendChild(doc.createElementNS(None,u'subsection1')) * How to create a text node: text1=doc.createTextNode(u'My first text') * Since we have no reference to subsection1. We start at reference to element1 element1.firstChild.appendChild(text1) * add second subelement: element1.appendChild(doc.createElementNS(None,u'subsection2')) * create next text element, and add it to subsection2 text2=doc.createTextNode(u'My second text') element1.lastChild.appendChild(text2) * We are working with references. To switch text in subsections, you could do: element1.firstChild.appendChild(text2) element1.lastChild.appendChild(text1) _______________________________________________ XML-SIG maillist - XML-SIG@xxxxxxxxxx http://mail.python.org/mailman/listinfo/xml-sig |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Include tarballs in pypi: 00008, Christian Theune |
|---|---|
| Next by Date: | Re: how to check "grammar" of xml using a schema or DTD withelementtree: 00008, Stephane Bortzmeyer |
| Previous by Thread: | Include tarballs in pypii: 00008, Christian Theune |
| Next by Thread: | Installation of XML package v0.8.3: 00008, SAROLI Andre Intérimaire |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |