I realize this is a very old question but it took me a little while to find 
the 'proper way' and/or 'pythonic+sqlalchemy' way to do it so here is the 
solution:

from sqlalchemy.sql.functions import GenericFunction
class XMLTypeFunc(GenericFunction):
    type=CLOB
    name='XMLType'
    identifier='XMLTypeFunc'


from sqlalchemy.types import TypeDecorator
from lxml import etree #make this safe for built-in etree as well - Matt
class XMLType(TypeDecorator):

    impl = CLOB
    type = 'XMLTYPE' #etree.Element

    def get_col_spec(self):
        return 'XMLTYPE'

    def bind_processor(self, dialect):
        def process(value):
            if value is not None:
                return etree.tostring(value, encoding='UTF-8', 
pretty_print='True')
                #return etree.dump(value)
            else:
                return None
        return process

    def process_result_value(self, value, dialect):
        if value is not None:
            value = etree.fromstring(value)
        return value

    def bind_expression(self, bindvalue):
        return XMLTypeFunc(bindvalue)


On Sunday, September 12, 2010 at 6:23:16 AM UTC-4, millsks wrote:
>
> I was curious if there were any way to pull a XMLType field into a 
> python field when using declarative.  I have been able to do it with 
> cx_Oracle by executing a query using XMLType.getClobVal and would 
> probably work with sqlalchemy's manual query system too, but would 
> love it if I could use it as part of an ORM object as an actual column/ 
> field or as a relationship. 
>
> If this is possible where should I look to learn how to integrate it 
> into our sqlalchemy code?

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to