File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes How to store whole XML in a table of a database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to store whole XML in a table of a database" Watch "How to store whole XML in a table of a database" New topic

How to store whole XML in a table of a database

Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 411
I have an XML created dynamically and now i want to store the whole xml (including tags as well as data) but the problem is that i cannot store the whole xml as string in any database due to the maximum limit of char datatypes which is generally(255)
so i thought that i should store it as blob datatype but problem is that i donot know whether i have to serialize my xmldocument before stoeing it in table or will blob datatype will accept my xml object as it is.I m very confused
Ajit kalambela will u throw some light on my confusion as u are a moderator.any other fellow can u give some idea???
Thankx in advance

SCJP,SCWCD,SCBCD<br />If Opportunity doesn't knock then build the door
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Your XML document is just a chunk of textual data. You don't mention what database you're using but from the sound of it a CLOB would be most appropriate. No need for serialization.
As an aside -- there are roughly three ways you can store an XML file in a database.

  • As a flat text field, without attempt to represent the structure of the XML file in the database. This is what you are planning to do.
  • As structured data in a set of tables, each table representing an XML construct (e.g. tag, attribute, cdata). This is how many generic XML database tools work. Although storage is structured, the database schema reflects the structure of XML itself rather than the structure of your file.
  • Completely decomposed into a set of tables, which between them represent your DTD (or rather XML Schema). The structure of your XML document is directly reflected in the database.

  • Of course, you can mix and match between these three. Which alternative you choose depends on what you want to do with the data -- the first is most appropriate if you just want to store the file, the last is most appropriate if you need to process or query the data on the same footing with all other database data.
    - Peter

    [This message has been edited by Peter den Haan (edited March 11, 2001).]
Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 411
Hi Peter
I m not using any schema with my XML Document
so thats why i did not get (or understood) your third option
and as per your suggestion of implementing in first way
so as a flat text field how can i store the xml document object in a table
will it not say for datatype incompatibility as clob datatype and my xmldocument object donot have an datatype
can u Please elaborate it little further I will be thankful to you
regarding your database question it is either SQL Server7.0
or Oracle 8.0 (not finalised yet)
David Freels
Ranch Hand

Joined: Feb 01, 2001
Posts: 102
Open an input stream to the XML file and using PreparedStatement, pstmt.setAsciiStream(1, <stream from file> );, this will insert the file into the database.
Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 411
This function of PreparedStatement has a third paramater
called length but how can we know the length of InputStream of a file which is dynamically created??
and there is one more function in PreparedStatement called
setBlob(int x,Blob y)
will it help
if yes then do we have to give the xmlobject name as a Blob argument in this function???

[This message has been edited by Gaurav Chikara (edited March 12, 2001).]
I agree. Here's the link:
subject: How to store whole XML in a table of a database
It's not a secret anymore!