Home | Previous Page | Next Page   Performing Database Operations > Storing and Retrieving XML Documents >

Inserting Data Examples

The examples in this section illustrate converting XML documents to formats acceptable for insertion into Informix database columns.

XMLtoString() Examples

The following example converts three XML documents to character strings and then uses the strings as parameter values in an SQL INSERT statement:

PreparedStatement p = conn.prepareStatement("insert into tab
   values(?,?,?)");
p.setString(1, UtilXML.XMLtoString("/home/file1.xml"));
p.setString(2, UtilXML.XMLtoString("http://server/file2.xml");
p.setString(3, UtilXML.XMLtoString("file3.xml");

The following example inserts an XML file into an LVARCHAR column. In this example, tab1 is a table created using the SQL statement:

create table tab1 (col1 lvarchar);

The code is:

try
   {
   String cmd = "insert into tab1 values (?)";
   PreparedStatement pstmt = conn.prepareStatement(cmd);
   pstmt.setString(1, UtilXML.XMLtoString("/tmp/x.xml"));
   pstmt.execute();
   pstmt.close();
   }
    catch (SQLException e)
   {
   // Error handling
   }

XMLtoInputStream() Example

The following example inserts an XML file into a text column. In this example, table tab2 is created using the SQL statement:

create table tab2 (col1 text);

The code is:

try
   {
   String cmd = "insert into tab2 values (?)";
   PreparedStatement pstmt = conn.prepareStatement(cmd);
   pstmt.setAsciiStream(1, UtilXML.XMLtoInputStream("/tmp/x.xml"),
      (int)(new File("/tmp/x.xml").length()));
    pstmt.execute();
    pstmt.close();
    }
    catch (SQLException e)
    {
    // Error handling
    }
Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]