Example: Referencing XML host variables in embedded SQL applications

You can create XML host variables in embedded SQL applications so that you can read and process XML data.

The following sample applications demonstrate how to reference XML host variables in C and COBOL.

Example: Embedded SQL C application:

The following code example has been formatted for clarity: 
EXEC SQL BEGIN DECLARE; 
  SQL TYPE IS XML AS CLOB( 10K ) xmlBuf;
  SQL TYPE IS XML AS BLOB( 10K ) xmlblob; 
  SQL TYPE IS CLOB( 10K ) clobBuf; 
EXEC SQL END DECLARE SECTION; 

// as XML AS CLOB
// The XML value written to xmlBuf will be prefixed by an XML declaration 
// similar to: <?xml version = "1.0" encoding = "ISO-8859-1" ?>
// Note: The encoding name will depend upon the application codepage
EXEC SQL SELECT xmlCol INTO :xmlBuf 
   FROM myTable 
   WHERE id = '001'; 
EXEC SQL UPDATE myTable 
   SET xmlCol = :xmlBuf 
   WHERE id = '001'; 

// as XML AS BLOB 
// The XML value written to xmlblob will be prefixed by an XML declaration 
// similar to: <?xml version = "1.0" encoding = "UTF-8"?>
EXEC SQL SELECT xmlCol INTO :xmlblob 
   FROM myTable 
   WHERE id = '001'; 
EXEC SQL UPDATE myTable 
   SET xmlCol = :xmlblob 
   WHERE id = '001'; 

// as CLOB 
// The output will be encoded in the application character codepage,
// but will not contain an XML declaration
EXEC SQL SELECT XMLSERIALIZE (xmlCol AS CLOB(10K)) INTO :clobBuf 
   FROM myTable 
   WHERE id = '001'; 
EXEC SQL UPDATE myTable 
   SET xmlCol = XMLPARSE (:clobBuf PRESERVE WHITESPACE) 
   WHERE id = '001'; 

Example: Embedded SQL COBOL application:

The following code example has been formatted for clarity: 
EXEC SQL BEGIN DECLARE SECTION END-EXEC. 
  01 xmlBuf USAGE IS SQL TYPE IS XML as CLOB(5K).
  01 clobBuf USAGE IS SQL TYPE IS CLOB(5K).
  01 xmlblob  USAGE IS SQL TYPE IS BLOB(5K).
EXEC SQL END DECLARE SECTION END-EXEC.

* as XML
EXEC SQL SELECT xmlCol INTO :xmlBuf 
   FROM myTable 
   WHERE id = '001' END-EXEC.
EXEC SQL UPDATE myTable 
   SET xmlCol = :xmlBuf 
   WHERE id = '001' END-EXEC.

* as BLOB
EXEC SQL SELECT xmlCol INTO :xmlblob 
   FROM myTable 
   WHERE id = '001' END-EXEC.
EXEC SQL UPDATE myTable 
   SET xmlCol = :xmlblob 
   WHERE id = '001' END-EXEC.

* as CLOB
EXEC SQL SELECT XMLSERIALIZE(xmlCol AS CLOB(10K)) INTO :clobBuf 
   FROM myTable 
   WHERE id= '001' END-EXEC.
EXEC SQL UPDATE myTable 
   SET xmlCol = XMLPARSE(:clobBuf) PRESERVE WHITESPACE 
   WHERE id = '001' END-EXEC.