I need to insert, delete and update data using a xml file. I have heard of some methods like xpath, XStream or JDOM for doing this. I don't know anything except the names of this methods for use a xml like a database. Which one is easier to learn?
-
possible duplicate of [Best XML parser for Java](http://stackoverflow.com/questions/373833/best-xml-parser-for-java) – assylias Mar 21 '12 at 23:15
-
What do you mean with "using an xml file". The data should be inserted into an xml file, or the data to insert is from an xml file? – meriton Mar 21 '12 at 23:16
-
The data should be inserted into an xml file. – Aikanáro Mar 21 '12 at 23:18
-
It sounds like the OP wants to use an XML file as a database – Chris Thompson Mar 21 '12 at 23:33
3 Answers
Databases and XML are rather different beasts. XML is a sequential, document-centric file format, i.e. XML files can not be updated without writing the entire file back to disk. Databases on the other hand allow to efficiently insert, update and delete individual records without touching any other records.
You can not achieve that property when implementing a "database" using an XML file, so this approach will be very inefficient if you have a non-trivial amount of data, and update only a few records at a time.
That said, a relatively simple way to map data from and to XML is JAXB. A starting point might be Chapter 17 of the Java EE 5 Tutorial. You probably have no need for a schema, and simply annotate the classes you want to write to / read from XML with JAXB annotations.

- 68,356
- 14
- 108
- 175
I'm not sure that XML would make a good "database". XML is hierarchical; relational databases are set-based.
XML XPath is not the same thing as SQL DQL.
Are you trying to duplicate the LINQ idea of abstracting away the source of the data?

- 305,152
- 44
- 369
- 561
It depends very much on how you want to handle persistence of updates. If the data is small enough that you can consider writing the whole data back from memory to disk, then you could use a DOM-like approach (i.e. store the whole XML in memory), perhaps with a processor such as Saxon that supports XQuery and XQuery updates against in-memory XML. But this is only "like a database" to a very limited extent: it doesn't give you transactions, locking, recovery, atomicity, or any of the management capabilities that people associate with databases, let alone the scalability to handle large data volumes. If you want a real database, choose an XML database like eXist (open source) or MarkLogic (commercial).

- 156,231
- 11
- 92
- 164