Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use XML as database in Java

Tags:

java

xml

I want to use XML file as database. Where I want to store an ID and its correspondingly a String path. Like:

<item>
     <Id>id</Id>
     <Path>path</Path>
</item> 

or like that. Now in runtime a Name-Value Collection will load this data stored in XML document and check the Id against some data and will be processed accordingly and also update (that is change path of same Id)and delete(removal of an existing item) of items is needed in that XML file. Hope I make you able to understand my idea! I have no code to show as this is in conceptual level. What I need is that how can I achieve this, is there any tutorial I can read or API available which is able to do this? Thank you. I am in JAVA.

like image 290
Tapas Bose Avatar asked Dec 12 '22 16:12

Tapas Bose


2 Answers

On the other hand you are closer to XML if you use XML-Database Systems, which offer XML database APIs. Like Exist or Apache Xindice.

like image 180
Costis Aivalis Avatar answered Dec 15 '22 05:12

Costis Aivalis


I'll assume this is actually a good idea for your application - that's a whole different discussion.

I would treat this as three separate problems:

  • Loading the XML file from disk and storing it in an appropriate data structure (such as a Map<String, String>)
  • Manipulating the collection appropriately (fetching, adding or removing entries)
  • Saving the XML file back to disk

Most of your code should probably be unaware that your collection will be stored in XML.

There are various XML APIs available for Java - the built-in ones tend to be quite a pain to use. I don't know what the latest and greatest ones are, but historically JDOM has proved fairly simple.

Note that your choice of in-memory collection will depend on your requirements - is the ordering important, for example?

like image 27
Jon Skeet Avatar answered Dec 15 '22 05:12

Jon Skeet