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

Retrieving Data Examples

The following examples illustrate retrieving data from Informix database columns and converting the data to formats acceptable to XML parsers.

StringtoDOM() Example

This example operates under the assumption that xmlcol is a column of type lvarchar that contains XML data. The data could be fetched and converted to DOM with the following code:

ResultSet r = stmt.executeQuery("select xmlcol from table where
      ...");
while (r.next()
    {
    Document doc= UtilXML.StringtoDOM(r.getString("xmlcol"));
    // Process 'doc'
    }

InputStreamtoDOM() Example

The following example fetches XML data from a text column into a DOM object:

try
    {
    String sql = "select col1 from tab2";
    Statement stmt = conn.createStatement();
    ResultSet r = stmt.executeQuery(sql);
     while(r.next())
        {
        Document doc = UtilXML.InputStreamtoDOM(r.getAsciiStream(1));
        }
    r.close();
    }
    catch (Exception e)
    {
    // Error handling
    }

getInputSource() Examples

This example retrieves the XML data stored in column xmlcol and converts it to an object of type InputSource; the InputSource object i can then be used with any SAX or DOM parsing methods:

InputSource i = UtilXML.getInputSource
   (resultset.getString("xmlcol"));

This example uses the implementation of Sun's JAXP API, in xerces.jar, to parse fetched XML data in column xmlcol:

InputSource input = UtilXML.getInputSource(resultset.getString("xmlcol"));
SAXParserFactory f = SAXParserFactory.newInstance();
SAXParser parser = f.newSAXParser();
parser.parse(input);

In the examples that follow, tab1 is a table created using the SQL statement:

create table tab1 (col1 lvarchar); 

The following example fetches XML data from an LVARCHAR column into an InputSource object for parsing. This example uses SAX parsing by invoking the parser at org.apache.xerces.parsers.SAXParser.

try
    {
    String sql = "select col1 from tab1";
    Statement stmt = conn.createStatement();
    ResultSet r = stmt.executeQuery(sql);
    Parser p = ParserFactory.makeParser("org.apache.xerces.parsers.SAXParser");
    while(r.next())
        {
        InputSource i = UtilXML.getInputSource(r.getString(1));
        p.parse(i);
        }
    r.close();
    }
    catch (SQLException e)
    {
    // Error handling
    }

The following example fetches XML data from a text column into an InputSource object for parsing. This is the same example as the previous one, but it uses JAXP factory methods instead of the SAX parser to analyze the data.

try
    {
    String sql = "select col1 from tab2";
    Statement stmt = conn.createStatement();
    ResultSet r = stmt.executeQuery(sql);
    SAXParserFactory factory = SAXParserFactory.newInstance();
    Parser p = factory.newSAXParser();
    while(r.next())
        {
        InputSource i = UtilXML.getInputSource(r.getAsciiStream(1));
        p.parse(i);
        }
    r.close();
    }
    catch (Exception e)
    {
    // Error handling
    }
Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]