Search code examples
pythonxmloracle-databasepandasclob

Python, Oracle DB, XML data in a column, fetching cx_Oracle.Object


I am using python to fetch data from Oracle DB. All the rows have a column which has XML data. When I print the data fetched from Oracle DB using python, the column with XML data is printed as - cx_Oracle.OBJECT object at 0x7fffe373b960 etc. I even converted the data to pandas data frame and still the data for this columns is printed as cx_Oracle.OBJECT object at 0x7fffe373b960. I want to access the key value data stored in this column(XML files).


Solution

  • Please read inline comments.

    cursor = connection.cursor() # you know what it is for
    
    # here getClobVal() returns whole xml. It won't work without alias I don't know why.
    query = """select a.columnName.getClobVal() from tablename a""" 
    
    cursor.execute(query) #you know what it is for
    
    result = cursor.fetchone()[0].read() # for single record
    
    result = cursor.fetchall() # for all records
    for res in result:  
        print res[0].read()