[XML-SIG] python xml parser

Mike Brown mike at skew.org
Thu Mar 4 17:54:17 EST 2004

Dave Kuhlman wrote:
> On Tue, Mar 02, 2004 at 04:21:57PM -0500, J. Xu wrote:
> > How can I pass stdout of processes, or output stream of network channels 
> > (like ssh) to a python xml parser (either through DOM or SAX) so that 
> > xml can be used as the communication tool among different components of 
> > the software? I think the standard python xml reader expects a file-like 
> >   object or string as the input, but not sure if it can take a stream. 
> > Any suggestions are highly appreciated.
> The minidom parser in the Python standard library will take a file
> name or a file object.  From
> http://www.python.org/doc/current/lib/module-xml.dom.minidom.html:
>     parse(filename_or_file, parser)
>         Return a Document from the given input. filename_or_file
>         may be either a file name, or a file-like object. parser,
>         if given, must be a SAX2 parser object. This function will
>         change the document handler of the parser and activate
>         namespace support; other parser configuration (like setting
>         an entity resolver) must have been done in advance.

J. Xu -

Yes, as you guessed and as Dave mentions, a file-like object is all you need
on the XML reading side.

You can obtain the stdout stream of a process as a file-like object when you
create the process via os.popen(), os.popen2(), os.popen3(), os.popen4() --
see the os module docs for info on how to use those functions.

The method for obtaining a file-like object that wraps a network stream will
depend on the library/software you are using. It may provide an API that will
give you a file-like object, or a socket object, or it may not give you any
hooks at all.

Your task will be to figure out what access (if any) you have to the network
stream, and then if it's not a file-like object, you'll need to wrap it in
one. I believe the main requirement for a file-like object, as far as the
Python XML reading tools are concerned, is just that is have a read() method
that supplies all the bytes of the XML to be parsed.


More information about the XML-SIG mailing list