[XML-SIG] sax2 parsing from a string
Sam Brauer
sam@webslingerZ.com
Wed, 26 Sep 2001 12:49:53 -0400 (EDT)
Can someone give me a brief example showing how to create a
namespace-aware sax2 parser and use it to parse a string containing an
XML document?
I'm having no luck so far...
Here's the sort of thing I'm trying now:
import cStringIO
import xml.sax
import xml.sax.handler
import xml.sax.saxutils
import xml.sax.xmlreader
xmlstring = """<?xml version="1.0"?>
<page>
<foo test='123'>
Hello World
</foo>
</page>"""
myhandler = xml.sax.saxutils.XMLGenerator()
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, 1)
parser.setContentHandler(myhandler)
inputsource = xml.sax.xmlreader.InputSource()
inbuffer = cStringIO.StringIO()
inbuffer.write(xmlstring)
inbuffer.seek(0)
inputsource.setByteStream(inbuffer)
parser.parse(inputsource)
parser.close()
Here's the output I get:
<?xml version="1.0" encoding="iso-8859-1"?>
<page>Traceback (most recent call last):
File "./saxtest.py", line 25, in ?
parser.parse(inputsource)
File
"/usr/local/lib/python2.0/site-packages/_xmlplus/sax/expatreader.py", line
43, in parse
xmlreader.IncrementalParser.parse(self, source)
File "/usr/local/lib/python2.0/site-packages/_xmlplus/sax/xmlreader.py",
line 121, in parse
self.feed(buffer)
File
"/usr/local/lib/python2.0/site-packages/_xmlplus/sax/expatreader.py", line
87, in feed
self._parser.Parse(data, isFinal)
File "extensions/pyexpat.c", line 522, in CharacterData
TypeError: not enough arguments; expected 4, got 2
If I replace the line:
inputsource.setByteStream(inbuffer)
with:
inputsource.setCharacterStream(inbuffer)
I get:
Traceback (most recent call last):
File "./saxtest.py", line 23, in ?
parser.parse(inputsource)
File
"/usr/local/lib/python2.0/site-packages/_xmlplus/sax/expatreader.py", line
38, in parse
source = saxutils.prepare_input_source(source)
File "/usr/local/lib/python2.0/site-packages/_xmlplus/sax/saxutils.py",
line 369, in prepare_input_source
if os.path.isfile(sysid):
File "/usr/local/lib/python2.0/posixpath.py", line 192, in isfile
st = os.stat(path)
TypeError: stat, argument 1: expected string, None found
I'm using PyXML-0.6.6 and Python 2.0.
Also (on a tangent), I think in xml.sax.saxutils.XMLGenerator and
xml.sax.saxutils.XMLFilterBase that the characters() and
ignorableWhitespace() methods need to have 4 arguments instead of 2...
For example:
def characters(self, content, start, length):
self._out.write(escape(content[start:start+length]))
instead of:
def characters(self, content):
self._out.write(escape(content))
But I may be wrong...
Thanks for any help,
Sam