aspose file tools*
The moose likes Java in General and the fly likes How to store an XML file as a string in database and retrieve in Java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to store an XML file as a string in database and retrieve in Java code" Watch "How to store an XML file as a string in database and retrieve in Java code" New topic
Author

How to store an XML file as a string in database and retrieve in Java code

Sarah Raf
Ranch Hand

Joined: May 08, 2011
Posts: 35

Hello All,
I need help regarding XML file storage in Database. Which Database can be used to save the complete XML file as a string? and in my Java code I need to retrieve this String and parse it using JAXB. Can someone please help me out.. Which database do i need to use?
K Abhijit
Ranch Hand

Joined: Mar 03, 2008
Posts: 88
Are you looking for any realtime project or just for learning purpose?

There are many databases that support this feature; well I can tell you what i have worked on
Use Oracle 9.2 onwards (i think even 8 should also support but i never worked on that)
declare that XML column you want to store as String as CLOB
use Oracle driver (say ojdbc.jar) and use normal JDBC api

let me know incase you need any specific information



“The difference between 'involvement' and 'commitment' is like an eggs-and-ham breakfast: the chicken was 'involved' - the pig was 'committed'.”
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

In case you use Db2 , they have XML Datatype , value can be queries using XPATH .

in this case no need to parse the xml content in java , Xpath will be done in Db2 server memory .

Not sure about other Database vendors


Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Sarah Raf
Ranch Hand

Joined: May 08, 2011
Posts: 35

Thanks a lot for the Help
Abhijit - I am looking for a realtime project. I want to use an open source. Currently I am working on Derby embedded driver.
Ganesh - I want to store around 25 XML files, and these file will be called as and when required. These XML files should be saved as it is. I am using JAXB to parse them. So basically my requirement is,
1. XML to be saved in the database,
2. Retrieve the XML as an XML file itself.
So how can I proceed? Once again thanks for the help
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

If you use JAXB for un-marhsalling the XML data from DB .You can use Varchar .( if db you are using supports XML , then prefer XML data type)

Not sure when you use CLOB quering based on contents may take considerable amount of time .
Sarah Raf
Ranch Hand

Joined: May 08, 2011
Posts: 35

Thanks Ganesh...
Yes I am using Unmarshall command. So as per your post, I should try saving the XML file in a column, with the column data type as varchar?
Then in that case Derby can support the same?

Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Derby : Indexes are not supported for columns defined on CLOB, BLOB, and LONG VARCHAR data types.

Derby Supported Data Types

Derby doesnt support XML data type , if you use CLOB ,creation of index is restricted ( hence search will become late)

So i prefer VARCHAR(???) and create the index for this column .

Good Luck
Sarah Raf
Ranch Hand

Joined: May 08, 2011
Posts: 35

Thanks a ton Ganesh
Will try it and let you know if I need any more Help...
p Gpune
Greenhorn

Joined: May 09, 2011
Posts: 2
Hi Sarah Raf,

i do not know your exact business case behind storing xml file into db but you should not store your data(xml) as file into database for security purpose.

What i can suggest it !! please convert xml to java object or xml to encryped file and store it into db.
Sarah Raf
Ranch Hand

Joined: May 08, 2011
Posts: 35

Hi p Gpune,
I don't want to put the XML content, rather I want to save the XML file as a String in the db, and retrieve the same string as a ".xml" file in my Java code and then parse it. Is there an easier way to do so in derby?

Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Hi Saraf ,

I think you mis understood what Gpune had mentioned .

he mentioned rather than storing raw xml as string , store the same content in serialised object and retrive the serilazed object , deserilalise the contents .
Sarah Raf
Ranch Hand

Joined: May 08, 2011
Posts: 35

Thanks a lot again
It worked.....

Now I have another problem thou,
I need to create an array of hashmap..
for example i want fetch data from the XML that I placed in the database and store it in a result set object, this contains around 25 to 30 number of rows. I want copy those resultset data to the Hashmap. and this Hashmap should be referred first when i need to check some data, in case the data doesnt exist in the Hashmap then the control should be passed to the database an then the again the data would be parsed. This data again has to be saved in the Hashmap.
So basically my Hashmap would be used as an interface. Is this possible? one more doubt, will the data that is stored in hashmap be accessible or would it be deleted every time I run my code?

Regards,
Sarah
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

What you are doing is concept called "Caching In Memory" .

If you are using in Web Appln , store this hashmap instance in Application scope .

In standalone , as far your instance is alive you have data in hashmap , write the code in such as way , none of other class have access to modify values in hashmap .
Sarah Raf
Ranch Hand

Joined: May 08, 2011
Posts: 35

Thanks for the Quick reply Ganesh.
Do I need to make my HashMap class as protected then?
Sarah Raf
Ranch Hand

Joined: May 08, 2011
Posts: 35

The Hashmap I want to create is for a standalone application.
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Sarah Raf wrote:Thanks for the Quick reply Ganesh.
Do I need to make my HashMap class as protected then?


How can you do this ? ( will you extends Hashmap bh any chance if so you do ,but for your requirement it doesnt makes sense for that effort ) .



In above scenario makes sure while you are retriving from DB , make all other threads in wait stage , else those threads will get the satle data or null values .

Hope it helps .

Concepts to use
Singleton , In memory Caching , Threading ...
Sarah Raf
Ranch Hand

Joined: May 08, 2011
Posts: 35

My code is such that, I am generating 3D objects in a JFrame, these objects data is in XML files. These XML files are placed inside the DB. Now what I want to do is, instead of referring to the DB every time I want to create an object, I just want to place the parsed data inside a Hashmap, so that it acts like a memory storage and also it would not affect the performance of the code.
I would definitely try your code. Thanks once again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to store an XML file as a string in database and retrieve in Java code
 
Similar Threads
retrieving data from a text file
Accessing Java Program(DOM Code) Through JSP
XML data collection
retrieving audio files from database
Hibernate xml file