[IronPython] pyexpat and IronPython

Sridevi Aishwariya srideviaishwariya at gmail.com
Tue Aug 8 06:47:02 CEST 2006


__slots__ = [
       "StartElementHandler",
       "EndElementHandler",
       "CharacterDataHandler",
       ]
gives an error

On 8/5/06, Mark Rees <mark.john.rees at gmail.com> 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 = reader.Value
>                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 <t-bruch at microsoft.com> 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: users-bounces at lists.ironpython.com
> >> [mailto:users-bounces at lists.ironpython.com] On Behalf Of Sridevi
> >> Sent: Thursday, August 03, 2006 5:41 AM
> >> To: users-ironpython.com at lists.ironpython.com
> >> 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
> >> users at lists.ironpython.com
> >> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> >> _______________________________________________
> >> users mailing list
> >> users at lists.ironpython.com
> >> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> >>
> >>
> > _______________________________________________
> > users mailing list
> > users at lists.ironpython.com
> > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> >
> >
>
> _______________________________________________
> users mailing list
> users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20060808/a9a08617/attachment.html>


More information about the Ironpython-users mailing list