[XML-SIG] an example of generating XML?

Thomas B. Passin tpassin@comcast.net
Tue, 10 Sep 2002 08:10:18 -0400


Sorry, I accidently cut one essentiial import line from my example.  Add
this line:

from xml.dom import implementation


Tom P


> [warren henning]
>
> > Could someone give a simple example of generating a valid, well-formed
XML
> > file using PyXML?
> >
> > Just show me the code to create something simple like:
> >
> > <?xml version="1.0"?>
> > <data>
> >     <node id="1" ack="blah">This is a test node.</node>
> >     <node id="2">So is this.</node>
> > </data>
> >
> > I want something that works.
> >
>
> You could be more complete and accurate about what you are asking for.
> Without a DTD, your document cannot be tested for validity.  And you do
not
> need pyXML to create your document - you can just create a string - so I
> assume that you really mean to create it using DOM.
>
> Here is a minimal example that works, though without error handling.  It
> uses pyXML 0.8 plus the corresponding version of 4Suite, on Windows2000.
> This code comes mainly from test_document.py in the xmldoc\test\dom
> directory, which is worth reading parts of.  It creates enough of your
> requested document so you can see how to complete it.
>
> Cheers,
>
> Tom P
>
> from xml.dom import Document
> from xml.dom.ext.Printer import PrintWalker,PrintVisitor
>
> EMPTY_NAMESPACE=None
>
> def build_doc():
>     dt = implementation.createDocumentType('','','')
>     doc = implementation.createDocument(EMPTY_NAMESPACE,None,dt);
>
>     e = doc.createElement('data')
>     doc.appendChild(e)
>
>     e2 = doc.createElement('node')
>     e2.setAttribute('id','1')
>     e.appendChild(e2)
>
>     return doc
>
> if __name__=='__main__':
>     doc=build_doc()
>
>     import sys
>     visitor=PrintVisitor(sys.stdout,'iso-8859-1','    ')
>     printer=PrintWalker(visitor,doc)
>     printer.run()
>
>
>
> _______________________________________________
> XML-SIG maillist  -  XML-SIG@python.org
> http://mail.python.org/mailman/listinfo/xml-sig
>