XML with Python - supposed to be simple? ;)

James Kew james.kew at btinternet.com
Sun Jul 21 19:23:57 EDT 2002


"Martin v. Loewis" <martin at v.loewis.de> wrote in message
news:m3it38lsxm.fsf at mira.informatik.hu-berlin.de...
> "Vladimir Cherepanov" <vovka_foreverREMOVE_THIS at hotmail.com> writes:
>
> > As a complete newbie to both XML and Python, I've been trying to
> > create (any!) XML document using DOM API in PyXML package. Very
> > simple task isn't it?

Yes. Well documented, though, it isn't. But the DOM interfaces and minidom
are documented to reasonable level in the Library Reference; much of the
rest of PyXML is sparsely documented, but it does install some reasonable
example code.

> If you insist on using the DOM, I recommend that you read the DOM spec
> first (from www.w3.org). You'll then learn that you can create
> Document instances by invoking createDocument on the
> DOMImplementation. For minidom, you get the DOM implementation by
> calling xml.dom.minidom.getDOMImplementation().

Or read the minidom documentation:
http://www.python.org/doc/current/lib/module-xml.dom.minidom.html describes
how to create a document node and populate it with children.

Once you've grokked the DOM interfaces -- xml.dom.Node as the base interface
for all nodes, specialised to Document, Element etc for specific node
types -- and that you create "floating" children by calling the appropriate
create method on the document node _then_ put them into the tree by calling
appendChild on their parent node, building DOMs does start becoming easier.

> > And what about serializing?

If you're using xml.dom.minidom: writexml(file) on the document node to
write it to an XML file; xml.dom.minidom.parse(filename_or_file) to read an
XML file and return a document node.

--
James Kew
james.kew at btinternet.com






More information about the Python-list mailing list