XML data retrieval in SQLJ applications
When you retrieve data from XML columns of a database table in an SQLJ application, the output data must be explicitly or implicitly serialized.
- java.sql.SQLXML (requires an SDK for Java™ Version 6 or later, and the IBM® Data Server Driver for JDBC and SQLJ version 4.0 or later)
- com.ibm.db2.jcc.DB2Xml (deprecated)
- String
- byte[]
- sqlj.runtime.AsciiStream
- sqlj.runtime.BinaryStream
- sqlj.runtime.CharacterStream
If the application does not call the XMLSERIALIZE function before data retrieval, the data is converted from UTF-8 to the external application encoding for the character data types, or the internal encoding for the binary data types. No XML declaration is added. If the host expression is an object of the java.sql.SQLXML or com.ibm.db2.jcc.DB2Xml type, you need to call an additional method to retrieve the data from this object. The method that you call determines the encoding of the output data and whether an XML declaration with an encoding specification is added.
Method | Output data type | Type of XML internal encoding declaration added |
---|---|---|
SQLXML.getBinaryStream | InputStream | None |
SQLXML.getCharacterStream | Reader | None |
SQLXML.getSource | Source | None |
SQLXML.getString | String | None |
DB2Xml.getDB2AsciiStream | InputStream | None |
DB2Xml.getDB2BinaryStream | InputStream | None |
DB2Xml.getDB2Bytes | byte[] | None |
DB2Xml.getDB2CharacterStream | Reader | None |
DB2Xml.getDB2String | String | None |
DB2Xml.getDB2XmlAsciiStream | InputStream | US-ASCII |
DB2Xml.getDB2XmlBinaryStream | InputStream | Specified by getDB2XmlBinaryStream targetEncoding parameter |
DB2Xml.getDB2XmlBytes | byte[] | Specified by DB2Xml.getDB2XmlBytes targetEncoding parameter |
DB2Xml.getDB2XmlCharacterStream | Reader | ISO-10646-UCS-2 |
DB2Xml.getDB2XmlString | String | ISO-10646-UCS-2 |
If the application executes the XMLSERIALIZE function on the data that is to be returned, after execution of the function, the data has the data type that is specified in the XMLSERIALIZE function, not the XML data type. Therefore, the driver handles the data as the specified type and ignores any internal encoding declarations.
#sql iterator XmlStringIter (int, String);
#sql [ctx] siter = {SELECT C1, CADOC from CUSTACC};
#sql {FETCH :siter INTO :row, :outString};
The String type is a character type, so the data is converted
from UTF-8 to the external encoding, which is the default JVM encoding,
and returned without any XML declaration.#sql iterator XmlByteArrayIter (int, byte[]);
XmlByteArrayIter biter = null;
#sql [ctx] biter = {SELECT c1, CADOC from CUSTACC};
#sql {FETCH :biter INTO :row, :outBytes};
The byte[] type
is a binary type, so no data conversion from UTF-8 encoding occurs,
and the data is returned without any XML declaration.#sql iterator SqlXmlIter (int, java.sql.SQLXML);
SqlXmlIter SQLXMLiter = null;
java.sql.SQLXML outSqlXml = null;
#sql [ctx] SqlXmlIter = {SELECT c1, CADOC from CUSTACC};
#sql {FETCH :SqlXmlIter INTO :row, :outSqlXml};
java.io.InputStream XmlStream = outSqlXml.getBinaryStream();
The
FETCH statement retrieves the data into the SQLXML object in UTF-8
encoding. The SQLXML.getBinaryStream stores the data in a binary stream.#sql iterator DB2XmlIter (int, com.ibm.db2.jcc.DB2Xml);
DB2XmlIter db2xmliter = null;
com.ibm.db2.jcc.DB2Xml outDB2Xml = null;
#sql [ctx] db2xmliter = {SELECT c1, CADOC from CUSTACC};
#sql {FETCH :db2xmliter INTO :row, :outDB2Xml};
byte[] byteArray = outDB2XML.getDB2XmlBytes("UTF-8");
The
FETCH statement retrieves the data into the DB2Xml object in UTF-8
encoding. The getDB2XmlBytes method with the UTF-8 argument adds an
XML declaration with a UTF-8 encoding specification and stores the
data in a byte array.