Return-Path: Delivered-To: apmail-db-derby-user-archive@www.apache.org Received: (qmail 22146 invoked from network); 25 Aug 2008 22:25:07 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 25 Aug 2008 22:25:07 -0000 Received: (qmail 24712 invoked by uid 500); 25 Aug 2008 22:25:04 -0000 Delivered-To: apmail-db-derby-user-archive@db.apache.org Received: (qmail 24681 invoked by uid 500); 25 Aug 2008 22:25:04 -0000 Mailing-List: contact derby-user-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: List-Id: Reply-To: "Derby Discussion" Delivered-To: mailing list derby-user@db.apache.org Received: (qmail 24670 invoked by uid 99); 25 Aug 2008 22:25:04 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Aug 2008 15:25:04 -0700 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: local policy) Received: from [69.5.75.124] (HELO elfyard.com) (69.5.75.124) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Aug 2008 22:24:06 +0000 Received: from [192.168.1.143] (s2-e1.30-0-atreidessoftwaregrupsrl2.fdx.ro [89.39.32.234] (may be forged)) by elfyard.com (8.12.11.20060308/8.12.11) with ESMTP id m7PMNX5R018003 for ; Mon, 25 Aug 2008 18:23:34 -0400 Message-ID: <48B330E5.2000807@elfyard.com> Date: Tue, 26 Aug 2008 01:23:33 +0300 From: Valentin Cozma User-Agent: Thunderbird 2.0.0.16 (X11/20080724) MIME-Version: 1.0 To: Derby Discussion Subject: Re: Saving XML into a database References: In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Checker-Version: SpamAssassin 2.63 (2004-01-11) on elfyard.com X-Virus-Checked: Checked by ClamAV on apache.org X-Old-Spam-Status: No, hits=0.0 required=5.0 tests=none autolearn=ham version=2.63 jay _ wrote: > 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. I am inserting xml as blob ( binary ) . works like a charm . example follows . -- write public void addFileVersion(int idFile, String autor, String comentarii, InputStream stream, int fileSize) throws SQLException { addVersionToFile.setInt(1, idFile); addVersionToFile.setString(2, autor); addVersionToFile.setString(3, comentarii); addVersionToFile.setBinaryStream(4, stream, fileSize); addVersionToFile.execute(); } -- read public void saveFileVersionToDisk(int idFile, File location) throws SQLException, IOException { InputStream result = null; getFileVersion.setInt(1, idFile); ResultSet rs = getFileVersion.executeQuery(); if(rs.next()) { result = rs.getBinaryStream("bytes"); } if(result != null) { OutputStream of = new FileOutputStream(location); FileUtils.copyFiles(result, of); } rs.close(); } -- table create table file_versions( id int NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), id_file int not null constraint FK_FILE_VERSIONS__files references files(id) on delete cascade, autor varchar(100) NOT NULL, comentarii varchar(1000) NOT NULL, bytes blob not null); > > 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 >