DB2 Version 10.1 for Linux, UNIX, and Windows

XML data handling in CLI applications - Overview

You can use the SQL_XML data type in CLI applications to retrieve and store XML data. This data type corresponds to the native XML data type of the DB2® database, which is used to define columns that store well-formed XML documents.

You can bind the SQL_XML type to the following C types: SQL_C_BINARY, SQL_C_CHAR, SQL_C_WCHAR, and SQL_C_DBCHAR. Use the default SQL_C_BINARY type, instead of character types, to avoid possible data loss or corruption, which could result from code page conversion when character types are used.

To store XML data in an XML column, bind a binary (SQL_C_BINARY) or character (SQL_C_CHAR, SQL_C_WCHAR, or SQL_C_DBCHAR) buffer that contains the XML value to the SQL_XML SQL type and execute the INSERT or UPDATE SQL statements. To retrieve XML data from the database, bind the result set to a binary (SQL_C_BINARY) or character (SQL_C_CHAR, SQL_C_WCHAR, or SQL_C_DBCHAR) type. You should use character types with caution because of encoding issues.

When an XML value is retrieved into an application data buffer, the DB2 server performs an implicit serialization on the XML value to convert it from its stored hierarchical form to the serialized string form. For character-typed buffers, the XML value is implicitly serialized to the application character code page that is associated with the character type.

By default, an XML declaration is included in the output serialized string. You can change this default behavior by setting the SQL_ATTR_XML_DECLARATION statement or connection attribute, or by setting the XMLDeclaration CLI/ODBC configuration keyword in the db2cli.ini file.

You can issue and execute XQuery expressions and SQL/XML functions in CLI applications. SQL/XML functions are issued and executed like any other SQL statements. You must add a prefix to the XQuery expressions with the not case sensitive keyword XQUERY, or you must set the SQL_ATTR_XQUERY_STATEMENT statement attribute for the statement handle that is associated with the XQuery expression.

Note: Starting with DB2 Version 9.7 Fix Pack 5, the SQL_XML data type is supported for DB2 for i V7R1 servers or later releases.