• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 35
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sarah Raf
Ranch Hand
Posts: 35
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton Ganesh
Will try it and let you know if I need any more Help...
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the Quick reply Ganesh.
Do I need to make my HashMap class as protected then?
 
Sarah Raf
Ranch Hand
Posts: 35
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Hashmap I want to create is for a standalone application.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 35
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic