XML file parsing with SAX

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Willem Ligtenberg

    #1

    XML file parsing with SAX

    I decided to use SAX to parse my xml file.
    But the parser crashes on:
    File "/usr/lib/python2.3/site-packages/_xmlplus/sax/handler.py", line 38, in fatalError
    raise exception
    xml.sax._except ions.SAXParseEx ception: NCBI_Entrezgene .dtd:8:0: error in processing external entity reference

    This is caused by:
    <!DOCTYPE Entrezgene-Set PUBLIC "-//NCBI//NCBI Entrezgene/EN"
    "NCBI_Entrezgen e.dtd">

    If I remove it, it parses normally.
    I've created my parser like this:
    import sys
    from xml.sax import make_parser
    from handler import EntrezGeneHandl er

    fopen = open("mouse2.xm l", "r")
    ch = EntrezGeneHandl er()
    saxparser = make_parser()
    saxparser.setCo ntentHandler(ch )
    saxparser.parse (fopen)

    And the handler is:
    from xml.sax import ContentHandler

    class EntrezGeneHandl er(ContentHandl er):
    """
    A handler to deal with EntrezGene in XML
    """

    def startElement(se lf, name, attrs):
    print "Start element:", name

    So it doesn't do much yet. And still it crashes...
    How can I tell the parser not to look at the DOCTYPE declaration.
    On a website:

    it states that the SAX parsers are not validating, so this error shouldn't
    even occur?

    Cheers,

    Willem
  • Uche Ogbuji

    #2
    Re: XML file parsing with SAX

    On Sat, 2005-04-23 at 15:20 +0200, Willem Ligtenberg wrote:[color=blue]
    > I decided to use SAX to parse my xml file.
    > But the parser crashes on:
    > File "/usr/lib/python2.3/site-packages/_xmlplus/sax/handler.py", line 38, in fatalError
    > raise exception
    > xml.sax._except ions.SAXParseEx ception: NCBI_Entrezgene .dtd:8:0: error in processing external entity reference
    >
    > This is caused by:
    > <!DOCTYPE Entrezgene-Set PUBLIC "-//NCBI//NCBI Entrezgene/EN"
    > "NCBI_Entrezgen e.dtd">
    >
    > If I remove it, it parses normally.
    > I've created my parser like this:
    > import sys
    > from xml.sax import make_parser
    > from handler import EntrezGeneHandl er
    >
    > fopen = open("mouse2.xm l", "r")
    > ch = EntrezGeneHandl er()
    > saxparser = make_parser()
    > saxparser.setCo ntentHandler(ch )
    > saxparser.parse (fopen)
    >
    > And the handler is:
    > from xml.sax import ContentHandler
    >
    > class EntrezGeneHandl er(ContentHandl er):
    > """
    > A handler to deal with EntrezGene in XML
    > """
    >
    > def startElement(se lf, name, attrs):
    > print "Start element:", name
    >
    > So it doesn't do much yet. And still it crashes...
    > How can I tell the parser not to look at the DOCTYPE declaration.
    > On a website:
    > http://www.devarticles.com/c/a/XML/P...-and-Python/1/
    > it states that the SAX parsers are not validating, so this error shouldn't
    > even occur?[/color]

    Just because it's not validating doesn't mean that the parser won't try
    to read the external entity.

    Maybe you're looking for

    """
    feature_externa l_ges
    Value: "http://xml.org/sax/features/external-general-entities"
    true: Include all external general (text) entities.
    false: Do not include external general entities.
    access: (parsing) read-only; (not parsing) read/write
    """

    Quote from:

    Source code: Lib/xml/sax/handler.py The SAX API defines five kinds of handlers: content handlers, DTD handlers, error handlers, entity resolvers and lexical handlers. Applications normally only nee...


    But you're on pretty shaky ground in any XML 1.x toolkit using a bogus
    DTDecl in this way. Why go through the hassle? Why not use a catalog,
    or remove the DTDecl?


    --
    Uche Ogbuji Fourthought, Inc.
    http://uche.ogbuji.net http://fourthought.com
    http://copia.ogbuji.net http://4Suite.org
    Use CSS to display XML, part 2 - http://www-128.ibm.com/developerwork...xmlcss2-i.html
    XML Output with 4Suite & AMara - http://www.xml.com/pub/a/2005/04/20/py-xml.html
    Use XSLT to prepare XML for import into OpenOffice Calc - http://www.ibm.com/developerworks/xml/library/x-oocalc/
    Schema standardization for top-down semantic transparency - http://www-128.ibm.com/developerwork...x-think31.html

    Comment

    • Willem Ligtenberg

      #3
      Re: XML file parsing with SAX

      I didn't make the XML file. And I don't like messing with other peoples
      data. So I just want my SAX parser to ignore it. I can't help if other
      people make it hard for me to read their xml file...

      On Sat, 23 Apr 2005 13:48:49 -0600, Uche Ogbuji wrote:
      [color=blue]
      > On Sat, 2005-04-23 at 15:20 +0200, Willem Ligtenberg wrote:[color=green]
      >> I decided to use SAX to parse my xml file.
      >> But the parser crashes on:
      >> File "/usr/lib/python2.3/site-packages/_xmlplus/sax/handler.py", line 38, in fatalError
      >> raise exception
      >> xml.sax._except ions.SAXParseEx ception: NCBI_Entrezgene .dtd:8:0: error in processing external entity reference
      >>
      >> This is caused by:
      >> <!DOCTYPE Entrezgene-Set PUBLIC "-//NCBI//NCBI Entrezgene/EN"
      >> "NCBI_Entrezgen e.dtd">
      >>
      >> If I remove it, it parses normally.
      >> I've created my parser like this:
      >> import sys
      >> from xml.sax import make_parser
      >> from handler import EntrezGeneHandl er
      >>
      >> fopen = open("mouse2.xm l", "r")
      >> ch = EntrezGeneHandl er()
      >> saxparser = make_parser()
      >> saxparser.setCo ntentHandler(ch )
      >> saxparser.parse (fopen)
      >>
      >> And the handler is:
      >> from xml.sax import ContentHandler
      >>
      >> class EntrezGeneHandl er(ContentHandl er):
      >> """
      >> A handler to deal with EntrezGene in XML
      >> """
      >>
      >> def startElement(se lf, name, attrs):
      >> print "Start element:", name
      >>
      >> So it doesn't do much yet. And still it crashes...
      >> How can I tell the parser not to look at the DOCTYPE declaration.
      >> On a website:
      >> http://www.devarticles.com/c/a/XML/P...-and-Python/1/
      >> it states that the SAX parsers are not validating, so this error shouldn't
      >> even occur?[/color]
      >
      > Just because it's not validating doesn't mean that the parser won't try
      > to read the external entity.
      >
      > Maybe you're looking for
      >
      > """
      > feature_externa l_ges
      > Value: "http://xml.org/sax/features/external-general-entities"
      > true: Include all external general (text) entities.
      > false: Do not include external general entities.
      > access: (parsing) read-only; (not parsing) read/write
      > """
      >
      > Quote from:
      >
      > http://docs.python.org/lib/module-xml.sax.handler.html
      >
      > But you're on pretty shaky ground in any XML 1.x toolkit using a bogus
      > DTDecl in this way. Why go through the hassle? Why not use a catalog,
      > or remove the DTDecl?[/color]

      Comment

      • Uche Ogbuji

        #4
        Re: XML file parsing with SAX

        On 4/23/05, Willem Ligtenberg <wligtenberg@gm ail.com> wrote:[color=blue]
        > so that will be sax.handler.fea ture_external_g es = "false"[/color]

        Yes.
        [color=blue]
        > And it will work?[/color]

        Honestly, I'm not sure. It should, but I've found these edge cases a
        bit hard to predict in the Python built-in libs :-(
        [color=blue]
        > But what about using a catalog? I am very new to python and XML...[/color]

        Catalogs allow you to rewrite the IDs for entities and such. So if
        you had an XML file with an entity at a URL, but you were working
        offline, you could use a catalog to "redirect" the entity to a copy on
        your local filesystem.

        Problem, now that I think of it, is that I'm not sure you can specify
        an catalog in PySAX. You might instead have to override the method
        entityResolver in your handler (and be sure to ). See the example in
        listing 1 and and discussion here:

        www.XML.com,Textuality Services,Uche Ogbuji,Perspectives,Gems from the Mines: 2002 to 2003


        Good luck.

        --
        Uche Ogbuji Fourthought, Inc.
        http://uche.ogbuji.net http://fourthought.com
        http://copia.ogbuji.net http://4Suite.org
        Use CSS to display XML, part 2 -

        XML Output with 4Suite & Amara -
        http://www.xml.com/pub/a/2005/04/20/py-xml.htmlUse XSLT to prepare XML
        for import into OpenOffice Calc -

        Schema standardization for top-down semantic transparency -

        Comment

        Working...