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.
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:
Map<String, String>
)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?
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.
BaseX is a database XML, seams to deliver what you want to do!
http://basex.org/
This will scale badly as you have to rewrite the whole XML file whenever you change the data. Just imagine you have 100000 entries and consider how much work you will have to do when using this database.
As a serialized form of an in-memory hashmap it is a different matter, though, as HashMap lookups are very fast. However, the easiest and fastest way to serialize a hashmap is to use the built-in XMLSerializer in the standard libraries.
I prefer using Joox, a fluent api to manage dom in jquery like style. It has not dependencies, you can install with maven.
Just because java performance, It's better to use attributes than elements* (experience from coleages).
So I suggest you use your model in this way:
I created this class using Joox for you:
To use it, you can use:
*Please note that string templates use caps as default value,e.g. id="ID" *Another thing methods insertOrUpdate, and delete only works with attributes as columns. *You can use Joox methods to insert new lines, as shown.
This is the file produced:
This is the output in console:
My maven dependencies: