faqts : Computers : Programming : Languages : Python : Common Problems : XML

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

9 of 11 people (82%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How do you parse a string and not a file?

Mar 26th, 2002 21:16
Michael Chermside, Alwyn Schoeman,


Any time that you have a String but the function you are using calls for
a file -- whether you are reading or writing -- you should use either
the StringIO or cStringIO module.
The StringIO module
(http://python.org/doc/current/lib/module-StringIO.html) is the original
Python version, while the cStringIO module
(http://python.org/doc/current/lib/module-cStringIO.html) is a much
faster version written in C. Use StringIO if you need to use Unicode
(which is likely with XML), or you need to subclass the module, since
cStringIO can do neither of these. Use cStringIO in other cases since
it's faster.
A quick example:
    >>> text = """\
    <?xml version="1.0" encoding="UTF-8"?>
    <Outer>
      <Inner>some</Inner>
      <Inner>data</Inner>
      <Inner>here</Inner>
    </Outer>
    """
    >>> import xml.sax
    >>> class MyHandler(xml.sax.handler.ContentHandler):
            def startElement(self, name, attr):
                print 'starting element %s' % name
    >>> parser = xml.sax.make_parser()
    >>> parser.setContentHandler(MyHandler())
    >>> import StringIO
    >>> parser.parse( StringIO.StringIO(text) )
    starting element Outer
    starting element Inner
    starting element Inner
    starting element Inner