Re: XML in Jython: ElementTree?
2007-11-01 15:14:22 GMT
F.Y.I..
There has been support for Jython in the ElementTree subversion repository since 2005 in the form of a file named JavaXMLTreeBuilder.py
.
Below is a sample of its explicit use as a parameter with the version 1.2.7 of ElementTree, by passing a TreeBuilder instance to the ElementTree.parse as the default parser. Of course you can set it as the default parser in
ElementTree.py by modifying the DefaultParserAPI line to be:
parser_api = default_parser_api = DefaultParserAPI(parser=JavaXMLTreeBuilder())
Regards,
Anthony
----------------------------
89> jython
Jython 2.2.1 on java1.6.0_03
Type "copyright", "credits" or "license" for more information.
>>> from elementtree.ElementTree import ElementTree
>>> from
elementtree.JavaXMLTreeBuilder import TreeBuilder
>>> tree = ElementTree()
>>> tree
<elementtree.ElementTree.ElementTree instance 1>
>>> root = tree.parse('nsexample.xml', TreeBuilder())
>>> for each in root:
... each
...
<Element {http://purl.org/dc/elements/1.1/}Description at 2>
<Element {
http://purl.org/dc/elements/1.1/}Title at 3>
<Element {http://fourthought.com/timelog}MinIncrement at 4>
<Element {
http://fourthought.com/timelog}InvoiceNumber at 5>
>>>
>>>
>>> from elementtree.ElementTree import dump
>>> dump(tree)
<ns0:ClientInfo xmlns:ns0="
http://fourthought.com/timelog">
<ns1:Description xmlns:ns1="http://purl.org/dc/elements/1.1/">
Fourthought, Inc
</ns1:Description>
<ns1:Title xmlns:ns1="http://purl.org/dc/elements/1.1/">
Management Subcontracting
</ns1:Title>
<ns0:MinIncrement>0.25</ns0:MinIncrement>
<ns0:InvoiceNumber>7777</ns0:InvoiceNumber>
</ns0:ClientInfo>
>>>
------------------------------------------------------------------------- This SF.net email is sponsored by: Splunk Inc. Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________ Jython-users mailing list Jython-users <at> lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jython-users
I don't actually use the "imp" module yet, but presume I will have to at
some point to manage compiled code and really make my new import hook do
stuff properly?
So it looks like the general functionality for overriding what __import__
does is supported in Jython2.2 (maybe even earlier?) but the ihooks module
itself is still missing?
In my case, fortunately the general "__builtin__" functionality and "imp"
functionality is hopefully really all I need for now (I think). If I
understand it correctly now, ihooks just lets multiple modifiers of the
import mechanism coordinate their hooks in a cooperative fashion, or as the
comment there says: "Consistent use of this [ihooks] module will make it
possible to change the different mechanisms involved in loading modules
independently."
Hope this might help someone else understand the basics if they want to do
something similar. The comp.lang.python thread referenced above was what
pointed me in this direction.
--Paul Fernhout
Paul D. Fernhout wrote:
> Anyway, does Jython2.2 just support import hooks in general but not the
> ihooks module?
>
> Can I do an import hook written in Jython somehow just not using the ihooks
> module? Or do I need to make a Java class like dperez did?
>
> If I want to use the ihooks module, would I need to continue hacking away
> until I make a module that works?
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>
RSS Feed