Start of change

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.

The host expression or iterator data types that you can use to retrieve data from XML columns are:

  • java.sql.SQLXML (SQLJ Version 4.0)
  • 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.

The following table lists the methods that you can call to retrieve data from a java.sql.SQLXML or a com.ibm.db2.jcc.DB2Xml object, and the corresponding output data types and type of encoding in the XML declarations.

Table 1. SQLXML and DB2Xml methods, data types, and added encoding specifications
Method Output data type Type of XML internal encoding declaration added
SQLXML.getBinaryStream InputStream XML column CCSID encoding
SQLXML.getCharacterStream Reader None
SQLXML.getSource Source None
SQLXML.getString String None

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.

Suppose that you retrieve data from an XML column into a String host expression.

#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 and returned without any XML declaration.

Suppose that you retrieve data from an XML column into a byte[] host expression.

#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.

Suppose that you retrieve a document from an XML column into a java.sql.SQLXML host expression, but you need the data in a binary stream.

#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.

End of change