jython lacks working xml processing modules?

Discussion in 'Python' started by Jane Austine, Nov 21, 2003.

  1. Jane Austine

    Jane Austine Guest

    I'm trying to parse an xml file with jython (not through java parsers
    like xerces).

    I tried minidom in jython 2.1 and 2.2a but all failed.

    What can I do? The last resort would be using java parsers. Then how
    can I use them like python xml parsers? It seems like javadom and
    javasax has something to do, but I don't know how.

    Jane
     
    Jane Austine, Nov 21, 2003
    #1
    1. Advertising

  2. (Jane Austine) wrote:

    > I'm trying to parse an xml file with jython (not through java parsers
    > like xerces).


    If all else fails, try pxdom. It's a pure-Python DOM/parser, so it should work
    with Jython, but it's not going to be fast. At all.

    http://www.doxdesk.com/software/py/pxdom.html

    (BTW: new version coming this weekend, to catch up with the changes in the new
    DOM 3 CRs.)

    --
    Andrew Clover
    mailto:
    http://www.doxdesk.com/
     
    Andrew Clover, Nov 21, 2003
    #2
    1. Advertising

  3. Jane Austine

    Alan Kennedy Guest

    [Jane Austine]
    > I'm trying to parse an xml file with jython (not through java parsers
    > like xerces).
    >
    > I tried minidom in jython 2.1 and 2.2a but all failed.


    It's quite likely that your documents contained namespaces. The only
    parser supported in jython is "xmlproc", because it is pure python.
    However, "xmlproc" has some significant bugs in relation to namespace
    processing, IIRC from the last time I looked at it.

    > What can I do?


    1. Use a Java SAX2 parser, write a jython ContentHandler for it, build
    a Minidom from the events.
    2. Use a Java DOM processor (DOM4J, JDOM, etc), and let it build a DOM
    for you.

    It would probably be easier if you could give an outline of what you
    are trying to achieve. For example, do you really need to build an
    object model? Do you need to use xpath? Do you need to validate
    structures? Etc, etc.

    > The last resort would be using java parsers. Then how
    > can I use them like python xml parsers? It seems like javadom and
    > javasax has something to do, but I don't know how.


    If you want to know about using SAX events to build object models,
    check this old thread on c.l.py.

    http://groups.google.com/groups?hl=

    If you have any specific questions or face any specific problems, post
    some details.

    regards,

    --
    alan kennedy
    -----------------------------------------------------
    check http headers here: http://xhaus.com/headers
    email alan: http://xhaus.com/mailto/alan
     
    Alan Kennedy, Nov 21, 2003
    #3
  4. Jane Austine wrote:
    > I'm trying to parse an xml file with jython (not through java parsers
    > like xerces).
    >
    > I tried minidom in jython 2.1 and 2.2a but all failed.
    >
    > What can I do? The last resort would be using java parsers. Then how
    > can I use them like python xml parsers? It seems like javadom and
    > javasax has something to do, but I don't know how.


    There is a really goog xml toolkit wich even covers xslt and some other
    fancy stuff. Its called 4suite, and you can get it here:

    http://www.4suite.org/

    One of the authors, Uche Ogbuji, has some tutorials on working with it
    on developerworks.

    Diez
     
    Diez B. Roggisch, Nov 21, 2003
    #4
  5. Jane Austine

    Jane Austine Guest

    Alan Kennedy <> wrote in message news:<>...
    > [Jane Austine]
    > > I'm trying to parse an xml file with jython (not through java parsers
    > > like xerces).
    > >
    > > I tried minidom in jython 2.1 and 2.2a but all failed.

    >
    > It's quite likely that your documents contained namespaces.


    No.

    Jython 2.1 on java1.4.2-beta (JIT: null)
    Type "copyright", "credits" or "license" for more information.
    >>> from xml.dom import minidom
    >>> minidom.parseString('<tag>foobar</tag>')

    Traceback (innermost last):
    File "<console>", line 1, in ?
    File "C:\work\jython-2.1\Lib\xml\dom\minidom.py", line 913, in parseString
    File "C:\work\jython-2.1\Lib\xml\dom\minidom.py", line 900, in _doparse
    File "C:\work\jython-2.1\Lib\xml\dom\pulldom.py", line 251, in getEvent
    AttributeError: feed
    >>>



    > The only
    > parser supported in jython is "xmlproc", because it is pure python.


    I can't make it work.

    >>> import xml.sax,xml.dom.minidom
    >>> p=xml.sax.make_parser(["xml.sax.drivers2.drv_xmlproc"])
    >>> xml.dom.minidom.parseString('<tag>foobar</tag>',parser=p)

    Traceback (innermost last):
    File "<console>", line 1, in ?
    File "C:\work\jython-2.1\Lib\xml\dom\minidom.py", line 913, in parseString
    File "C:\work\jython-2.1\Lib\xml\dom\minidom.py", line 900, in _doparse
    File "C:\work\jython-2.1\Lib\xml\dom\pulldom.py", line 251, in getEvent
    AttributeError: feed

    > However, "xmlproc" has some significant bugs in relation to namespace
    > processing, IIRC from the last time I looked at it.
    >
    > > What can I do?

    >
    > 1. Use a Java SAX2 parser, write a jython ContentHandler for it, build
    > a Minidom from the events.
    > 2. Use a Java DOM processor (DOM4J, JDOM, etc), and let it build a DOM
    > for you.
    >
    > It would probably be easier if you could give an outline of what you
    > are trying to achieve. For example, do you really need to build an
    > object model? Do you need to use xpath? Do you need to validate
    > structures? Etc, etc.
    >


    I need the tree model of the xml document. So I'm trying to use DOM parsers.

    > > The last resort would be using java parsers. Then how
    > > can I use them like python xml parsers? It seems like javadom and
    > > javasax has something to do, but I don't know how.

    >
    > If you want to know about using SAX events to build object models,
    > check this old thread on c.l.py.
    >
    > http://groups.google.com/groups?hl=
    >
    > If you have any specific questions or face any specific problems, post
    > some details.
    >
    > regards,
     
    Jane Austine, Nov 21, 2003
    #5
  6. Jane Austine

    Alan Kennedy Guest

    [Alan Kennedy]
    >> The only
    >> parser supported in jython is "xmlproc", because it is pure python.


    [Jane Austine]
    > I can't make it work.
    >
    > >>> import xml.sax,xml.dom.minidom
    > >>> p=xml.sax.make_parser(["xml.sax.drivers2.drv_xmlproc"])
    > >>> xml.dom.minidom.parseString('<tag>foobar</tag>',parser=p)

    > Traceback (innermost last):
    > File "<console>", line 1, in ?
    > File "C:\work\jython-2.1\Lib\xml\dom\minidom.py", line 913, in parseString
    > File "C:\work\jython-2.1\Lib\xml\dom\minidom.py", line 900, in _doparse
    > File "C:\work\jython-2.1\Lib\xml\dom\pulldom.py", line 251, in getEvent
    > AttributeError: feed


    Hmm, pity about that. To be honest, I'm not going to try and make it
    work. Any time I need XML processing in jython, I use native Java xml
    parsers, such as Xerces. I think most jython users work the same way.

    [Alan Kennedy]
    >> It would probably be easier if you could give an outline of what you
    >> are trying to achieve. For example, do you really need to build an
    >> object model? Do you need to use xpath? Do you need to validate
    >> structures? Etc, etc.


    [Jane Austine]
    > I need the tree model of the xml document. So I'm trying to use DOM parsers.


    I kind of figured that. My focus with the question was "Why do you
    need a tree model"? Do you need to data extraction, structure
    validation, xpath/xslt, etc?

    If this helps, here is a jython snippet that creates a DOM using
    Apache Xerces.

    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    from java.io import StringReader

    import org.xml.sax as sax
    import org.apache.xerces.parsers.DOMParser as domparser

    if __name__ == "__main__":
    parser = domparser()
    document = """<doc><a href="http://www.python.org"/></doc>"""
    parser.reset()
    documentIS = sax.InputSource(StringReader(document))
    parser.parse(documentIS)
    domtree = parser.getDocument()
    results = domtree.getElementsByTagName('a')
    for ix in range(results.getLength()):
    print "Link found: uri=%s" % results.item(ix).getAttribute('href')

    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    HTH,

    --
    alan kennedy
    -----------------------------------------------------
    check http headers here: http://xhaus.com/headers
    email alan: http://xhaus.com/mailto/alan
     
    Alan Kennedy, Nov 21, 2003
    #6
  7. Jane Austine

    Uche Ogbuji Guest

    "Diez B. Roggisch" <> wrote in message news:<bpksk3$1nt1ov$-berlin.de>...
    > Jane Austine wrote:
    > > I'm trying to parse an xml file with jython (not through java parsers
    > > like xerces).
    > >
    > > I tried minidom in jython 2.1 and 2.2a but all failed.
    > >
    > > What can I do? The last resort would be using java parsers. Then how
    > > can I use them like python xml parsers? It seems like javadom and
    > > javasax has something to do, but I don't know how.

    >
    > There is a really goog xml toolkit wich even covers xslt and some other
    > fancy stuff. Its called 4suite, and you can get it here:
    >
    > http://www.4suite.org/
    >
    > One of the authors, Uche Ogbuji, has some tutorials on working with it
    > on developerworks.


    Thanks for the plug, but 4Suite has a lot of C code in it and is
    probably not an option for use in Java without some porting work.

    --
    Uche
    http://uche.ogbuji.net
     
    Uche Ogbuji, Nov 23, 2003
    #7
  8. Jane Austine

    Paul Boddie Guest

    (Jane Austine) wrote in message news:<>...
    > I'm trying to parse an xml file with jython (not through java parsers
    > like xerces).
    >
    > I tried minidom in jython 2.1 and 2.2a but all failed.
    >
    > What can I do? The last resort would be using java parsers. Then how
    > can I use them like python xml parsers? It seems like javadom and
    > javasax has something to do, but I don't know how.


    The Java parsers seem to work quite well with the xml.dom.javadom
    package. First, update your CLASSPATH with references to the necessary
    ..jar files - this can be a frustrating process, but I found that
    xercesImpl-2.5.0.jar and xml-apis.jar were a good combination:

    export CLASSPATH=.../xercesImpl-2.5.0.jar:.../xml-apis.jar

    Then, start jython and try the following:

    import xml.dom.javadom
    impl = xml.dom.javadom.XercesDomImplementation()
    # Use your own filename below!
    doc = impl.buildDocumentFile("example.xml")
    # Now, try some PyXML-style DOM properties and methods.
    doc.childNodes
    doc.childNodes[0].getAttribute("some-attr")

    I'd seen javadom lurking in PyXML before now, but it's a nice surprise
    to see that it works rather well, especially since I've never seen any
    evidence of anyone using it (as far as I remember).

    Paul
     
    Paul Boddie, Nov 24, 2003
    #8
  9. Jane Austine

    Guest

    在 2003å¹´11月24日星期一UTC+8下åˆ7æ—¶42分31秒,Paul Boddie写é“:
    > (Jane Austine) wrote in message news:&lt;&gt;...
    > &gt; I'm trying to parse an xml file with jython (not through java parsers
    > &gt; like xerces).
    > &gt;
    > &gt; I tried minidom in jython 2.1 and 2.2a but all failed.
    > &gt;
    > &gt; What can I do? The last resort would be using java parsers. Then how
    > &gt; can I use them like python xml parsers? It seems like javadom and
    > &gt; javasax has something to do, but I don't know how.
    >
    > The Java parsers seem to work quite well with the xml.dom.javadom
    > package. First, update your CLASSPATH with references to the necessary
    > .jar files - this can be a frustrating process, but I found that
    > xercesImpl-2.5.0.jar and xml-apis.jar were a good combination:
    >
    > export CLASSPATH=.../xercesImpl-2.5.0.jar:.../xml-apis.jar
    >
    > Then, start jython and try the following:
    >
    > import xml.dom.javadom
    > impl = xml.dom.javadom.XercesDomImplementation()
    > # Use your own filename below!
    > doc = impl.buildDocumentFile(&quot;example.xml&quot;)
    > # Now, try some PyXML-style DOM properties and methods.
    > doc.childNodes
    > doc.childNodes[0].getAttribute(&quot;some-attr&quot;)
    >
    > I'd seen javadom lurking in PyXML before now, but it's a nice surprise
    > to see that it works rather well, especially since I've never seen any
    > evidence of anyone using it (as far as I remember).
    >
    > Paul


    hi,do you know the PyXML whether can be supported by Jython ?
    Looking forward to your reply!
    Thanks
     
    , Jul 17, 2012
    #9
  10. , 17.07.2012 10:35:
    > hi,do you know the PyXML whether can be supported by Jython ?


    PyXML is a dead project, don't use it.

    You can use ElementTree in Jython, just as in Python.

    Stefan
     
    Stefan Behnel, Jul 17, 2012
    #10
  11. Jane Austine

    Matej Cepl Guest

    Matej Cepl, Jul 17, 2012
    #11
  12. Matej Cepl, 17.07.2012 11:39:
    > On 17/07/12 10:35, wrote:
    >>> &gt; I'm trying to parse an xml file with jython (not through java
    >>> parsers
    >>> &gt; like xerces).

    >
    > https://code.google.com/p/jython-elementtree/ ???


    Note that this ships with Jython 2.5.

    Stefan
     
    Stefan Behnel, Jul 17, 2012
    #12
  13. Jane Austine

    Guest

    在 2012å¹´7月17日星期二UTC+8下åˆ6æ—¶02分31秒,Stefan Behnel写é“:
    > Matej Cepl, 17.07.2012 11:39:
    > &gt; On 17/07/12 10:35, wrote:
    > &gt;&gt;&gt; &amp;gt; I&amp;#39;m trying to parse an xml file with jython(not through java
    > &gt;&gt;&gt; parsers
    > &gt;&gt;&gt; &amp;gt; like xerces).
    > &gt;
    > &gt; https://code.google.com/p/jython-elementtree/ ???
    >
    > Note that this ships with Jython 2.5.
    >
    > Stefan


    Got that,thanks!
    However,there is one project implemented by Python used PyXML and now my Jython project has to depend on it ,so I am afraid that if Jython doesn't support PyXML,then my jython project can not depend on the original Python project ,then my jython project maybe can not move on unless I find another project to take place of the original Python project.
     
    , Jul 18, 2012
    #13
  14. Jane Austine

    Guest

    在 2012å¹´7月17日星期二UTC+8下åˆ6æ—¶02分31秒,Stefan Behnel写é“:
    > Matej Cepl, 17.07.2012 11:39:
    > &gt; On 17/07/12 10:35, wrote:
    > &gt;&gt;&gt; &amp;gt; I&amp;#39;m trying to parse an xml file with jython(not through java
    > &gt;&gt;&gt; parsers
    > &gt;&gt;&gt; &amp;gt; like xerces).
    > &gt;
    > &gt; https://code.google.com/p/jython-elementtree/ ???
    >
    > Note that this ships with Jython 2.5.
    >
    > Stefan


    Got that,thanks!
    However,there is one project implemented by Python used PyXML and now my Jython project has to depend on it ,so I am afraid that if Jython doesn't support PyXML,then my jython project can not depend on the original Python project ,then my jython project maybe can not move on unless I find another project to take place of the original Python project.
     
    , Jul 18, 2012
    #14
  15. Jane Austine

    Matej Cepl Guest

    On 18/07/12 05:12, wrote:
    > However,there is one project implemented by Python used PyXML and now
    > my Jython project has to depend on it ,so I am afraid that if Jython
    > doesn't support PyXML,then my jython project can not depend on the
    > original Python project ,then my jython project maybe can not move on
    > unless I find another project to take place of the original Python
    > project.


    I think, if possible, such project should switch out of PyXML anyway. If
    you make them nice patch to port them to standard ElementTree (and as a
    side-effect make the project working with Jython), they will like you. I
    guess.

    Matěj
     
    Matej Cepl, Jul 18, 2012
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DBxGlock
    Replies:
    6
    Views:
    3,179
    Scott Allen
    Nov 18, 2005
  2. David McNab
    Replies:
    0
    Views:
    522
    David McNab
    Apr 9, 2004
  3. Robert
    Replies:
    5
    Views:
    2,525
    Steve Holden
    Feb 3, 2005
  4. Divya Prakash
    Replies:
    0
    Views:
    465
    Divya Prakash
    Dec 19, 2006
  5. Pinegar, Kent T

    FW: [Jython-users] Jython Licensing Question

    Pinegar, Kent T, Jul 24, 2008, in forum: Python
    Replies:
    0
    Views:
    373
    Pinegar, Kent T
    Jul 24, 2008
Loading...

Share This Page