From:
[EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Sridevi Aishwariya
Sent: Monday, August 07, 2006 9:47 PM
To: Discussion of IronPython
Subject: Re: [IronPython] pyexpat and IronPython
On 8/5/06, Mark Rees <[EMAIL PROTECTED]> wrote:
Hi,
Seo has already done a basic pyexpat wrapper. He posted about it in:
http://lists.ironpython.com/pipermail/users-ironpython.com/2005-November/001227.html
And it can be found at:
http://sparcs.kaist.ac.kr/~tinuviel/fepy/old/pyexpat.py
I have used this with ElementTree. There are limitations like iterparse
doesn't work and if the XML you are parsing contains an empty element
i.e <doc /> it fails. I have modified Seo's pyexpat.py to handle the
second limitation and have included the source in this email. I have
used this for my elementtree requirements but the XML I parse is simple
so there may be other as yet undiscovered issues.
Regards
Mark
Modified version of pyexpat from
http://sparcs.kaist.ac.kr/~tinuviel/fepy/old/pyexpat.py
import clr
clr.AddReference(" System.Xml")
from System.IO import MemoryStream, SeekOrigin, StreamWriter
from System.Xml import NameTable, XmlNamespaceManager, XmlParserContext,
XmlSpace
from System.Xml import XmlTextReader, XmlNodeType
def ErrorString(errno):
if errno > 0:
return "unknown error"
else:
return None
def ParserCreate(*args):
return xmlparser()
class xmlparser:
__slots__ = [
"StartElementHandler",
"EndElementHandler",
"CharacterDataHandler",
]
returns_unicode = False
def __init__(self):
self._stream = MemoryStream()
self._parser = self._make_parser()
def Parse(self, data, isfinal=False):
self._append_stream(data)
self._parse()
if isfinal:
self._stream.Close()
def _make_parser(self):
table = NameTable()
manager = XmlNamespaceManager(table)
parser = XmlParserContext(table, manager,
None, XmlSpace.None)
return parser
def _append_stream(self, data):
stream = self._stream
position = stream.Position
stream.Seek(0, SeekOrigin.End)
writer = StreamWriter(stream)
writer.Write(data)
writer.Flush()
stream.Position = position
def _parse(self):
reader = XmlTextReader(self._stream,
XmlNodeType.Element,
self._parser)
while reader.Read():
nodetype =
reader.NodeType
if nodetype ==
XmlNodeType.Element:
name = reader.Name
attributes = {}
while reader.MoveToNextAttribute():
attributes[reader.Name] = reader.Value
if hasattr(self, "StartElementHandler"):
self.StartElementHandler(name, attributes)
# Create EndElement event as XmlTextReader doesn't
# do this for empty elements
if reader.IsEmptyElement :
nodetype = XmlNodeType.EndElement
if nodetype ==
XmlNodeType.EndElement:
name = reader.Name
if hasattr(self, "EndElementHandler"):
self.EndElementHandler(name)
elif nodetype ==
XmlNodeType.Text:
data = "">
if hasattr(self, "CharacterDataHandler"):
self.CharacterDataHandler (data)
Monty Taylor wrote:
> You could write a Python or C# wrapper around System.XML that behaves
> like pyexpat and contribute it. :)
>
> Monty
>
> On 8/4/06, Bruce Christensen < [EMAIL PROTECTED]>
wrote:
>
>> Nope. :) pyexpat relies on Python extension modules written in C,
which
>> don't work with IronPython. However, you can use the classes in the
.NET
>> System.Xml namespace for parsing XML documents if you're writing new
>> code.
>>
>> --Bruce
>>
>> -----Original Message-----
>> From: [EMAIL PROTECTED]
>> [mailto:[EMAIL PROTECTED]]
On Behalf Of Sridevi
>> Sent: Thursday, August 03, 2006 5:41 AM
>> To: [email protected]
>> Subject: [IronPython] pyexpat and IronPython
>>
>> Iam unable to run pyexpat from IronPython
>> Is there any files i got to add for it
>>
>> _______________________________________________
>> users mailing list
>> [email protected]
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>> _______________________________________________
>> users mailing list
>> [email protected]
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>
> _______________________________________________
> users mailing list
> [email protected]
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
_______________________________________________
users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com