I outlined a solution to writing/reading to an Oracle XMLType column for Oracle 11g (specifically 11.2.0.1) if the XMLType column is stored as a CLOB, which is the default for 11.2.0.1.
REFERENCE ANSWER: Using oracle XMLType in a java spring hibernate scenario
In SQLDeveloper, if you view the SQL tab within a table definition (table DDL) it defines the XML columns storage in 11.2.0.1 like this:
XMLTYPE COLUMN "ATTRIBUTE_XML2" STORE AS BASICFILE CLOB
Problem: In Oracle 11.2.0.2, the default storage type for XMLType is Binary XML, which is preferred for performance reasons. It looks like this:
XMLTYPE COLUMN "ATTRIBUTE_XML2" STORE AS SECUREFILE BINARY XML
HOWEVER, once stored this way, my referenced solution no longer works for READING the XMLType column as XML from the database. It returns some garbage from xmlType.getStringVal(), which I assume is the oracle binary encoded XML.
Ultimate Problem I am unable to convert back from the oracle binxml format to a valid XML document.
I have tried the Oracle BinXMLProcessor, BinXMLStream, BinXMLDecoder and InfosetReader in various configurations as outlined by Oracle, but they seem to just read, rather than decode it - so it errors out.
Basic example within the nullSafeGet (see linked answer for context).
xmlType = (XMLType) rs.getObject(names[0]);
BinXMLProcessor xp = BinXMLProcessorFactory.createProcessor();
BinXMLStream bstr = xp.createBinXMLStream(xmlType.getInputStream());
BinXMLDecoder xdecode = bstr.getDecoder();
InfosetReader reader = xdecode.getReader();
while (reader.hasNext() && i < 25) {
i++;
reader.next();
logger.debug("1 v:" + reader.getValue() + ", s:" + reader.getStandalone() + ", type: " + reader.getEventType() + ", " + reader.getDataLength());
}
I tried similar approaches, replacing xmlType.getInputStream() with xmlType.getBlobVal(178), xmlType.getBytesValue(), but they all throw an exception or return garbage data.
Huh.