I’ve been reading this introduction to PyXml. The install process is really easy, for Gentoo there is already an ebuild, for MacOsX, it has to be downloaded and then just run: python setup.py install , that’s it.
import xml.dom.minidom
doc = xml.dom.minidom.Document()
#Create an element, it’ll be root element
elem = doc.createElementNS (”http://”,”rootElem”)
#append the child to the doc
doc.appendChild(elem)
#Create element named fill
elemfill = doc.createElement (”fill”)
#Append it to root child
elem.appendChild (elemfill)
#Create text element
text = doc.createTextNode(”Text node inside fill”)
#Append it to elemfill
elemfill.appendChild (text)
#create another element named filldos
elemfill = doc.createElement (”filldos”)
#text element
text = doc.createTextNode(”Text node inside fill dos”)
#append filldos to root element
elem.appendChild (elemfill)
#append text to filldos
elemfill.appendChild (text)
#print all root elements
for elem in doc.childNodes:
print elem.localName
print doc.toxml()
This test just creates some sample XML and prints it out. In this example XML I haven’t used any attributes, but I’ll just post later how to use them, the code would have been too long to post it. The PyXml API seems quite easy to learn. I’ll just keep posting notes about it as I go through it.