I've been struggling to find some decent examples of how to save XML into a Derby database.

I've managed to figure out that XML needs to be serialised into a different datatype before it can saved in Derby. I am using a CLOB. However, I am not quite sure how exactly to get the data into the XML column.

My sample code looks like this ....

        String expression = "INSERT INTO XML_DATA (DOCUMENT ) " +
                "VALUES (XMLPARSE( DOCUMENT CAST (? AS CLOB) PRESERVE WHITESPACE))";

        try
        {
            PreparedStatement preparedStatement = connection.prepareStatement(expression);
            // update statement and execute
//            Element ele = getHomeViewElement();
            logger.debug("xmlDocument = " + xmlDocument.getDocument().toString());
            preparedStatement.setString(1, xmlDocument.toString()); 

            preparedStatement.executeUpdate();

        } catch (SQLException e) {
            logger.warn(e);
        }

Using either the Element or the Document toString() methods result in an exception ....
java.sql.SQLException: Invalid XML DOCUMENT: Content is not allowed in prolog.

Can someone suggest what I am doing wrong here?

Cheers,
Jay


Be the filmmaker you always wanted to beŚlearn how to burn a DVD with Windows«. Make your smash hit