Store data in a XML file using the DOM API in JAXP
by Amit
Okay, if you are like me- a Java developer and think that XML is an easy solution to most of your light-weight, persistent data storage and retrieval needs, simply because of its structured nature, I shall briefly describe you how I use Java API for XML processing (JAXP)- DOM API to store and facilitate easy retrieval of data for my application.
Here it goes:
Say, I have some information which I want to store (from my application) as given in file mapping.xml
While coding up the Java class which will write the data into a XML file, the DOM approach is:
- Create the DOM representation of the data- this is the in-memory representation
- Write it to the XML file using classes from javax.xml.transform package which transforms the DOM representation into the persistent XML file
The code that I would use has two parts to it:
- First part deals with the scenario in which the XML file already exists
- Second part takes care of the scenario in which I am creating the XML file for the first time- this time the root element of the XML has to be created, which is the difference from the first part
The above logic can be easily implemented using:
String xmlFile = "mapping.xml";
File file = new File(xmlFile);
if (file.exists()) {
//then code for first part
}else
{
//code for second part
}
Here is the Java source code:
private void writeToXML(String arg1, String arg4, String arg7) throws IOException, SAXException, ParserConfigurationException {
String xmlFile = "mapping.xml";
File file = new File(xmlFile);
if (file.exists()) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Create the builder and parse the file
Document doc = factory.newDocumentBuilder().parse(xmlFile);
org.w3c.dom.Element mddevice = doc.getDocumentElement();
org.w3c.dom.Element new_mddevice = doc.createElement("mddevice");
new_mddevice.setAttribute("name", arg1);
org.w3c.dom.Element blockdevice = doc.createElement("device");
blockdevice.setAttribute("name", arg4);
new_mddevice.appendChild(blockdevice);
org.w3c.dom.Element mountpoint = doc.createElement("mountpoint");
new_mddevice.appendChild(mountpoint);
mountpoint.setTextContent(arg7);
mddevice.appendChild(new_mddevice);
try {
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Source src = new DOMSource(doc);
System.out.println("Starting the XML file creation");
Result dest = new StreamResult("mapping.xml");
aTransformer.transform(src, dest);
} catch (Exception e) {
System.out.println("Stream error" + e.getMessage());
}
} else {
try {
//Create instance of DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//Get the DocumentBuilder
DocumentBuilder docBuilder = factory.newDocumentBuilder();
//Create blank DOM Document
org.w3c.dom.Document doc = docBuilder.newDocument();
//create the root element
org.w3c.dom.Element root = doc.createElement("raidmap");
doc.appendChild(root);
org.w3c.dom.Element mddevice = doc.getDocumentElement();
org.w3c.dom.Element new_mddevice = doc.createElement("mddevice");
new_mddevice.setAttribute("name", arg1);
org.w3c.dom.Element blockdevice = doc.createElement("device");
blockdevice.setAttribute("name", arg4);
new_mddevice.appendChild(blockdevice);
org.w3c.dom.Element mountpoint = doc.createElement("mountpoint");
new_mddevice.appendChild(mountpoint);
mountpoint.setTextContent(arg7);
mddevice.appendChild(new_mddevice);
try {
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Source src = new DOMSource(doc);
Result dest = new StreamResult("mapping.xml");
aTransformer.transform(src, dest);
} catch (Exception e) {
System.out.println("Stream error" + e.getMessage());
}
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
Note: arg1, arg4, arg7 are the “data” that I will store in the XML file; please
change it to suit your needs
Related:
Good job Amit!Found the tutorial very useful, yet was wondering if there’s a similar one with SAX you would want to share.Bright
Interesting post. I have been wondering about this issue,so thanks for posting. I’ll likely be coming back to your blog. Keep up great writing. Find your great Travel News and sing the songs at Free Song Lyric or you can watch the drama at Korea Drama Online one of great korea drama is A Love to Kill if you go to travel to Indonesia learn Learn Indonesia Language first! And find your home cari rumah or make a blog Belajar membuat Blog find your home again rumah dijual and again at jual rumah the point is cari rumah, jual rumah, rumah dijual, download youtube and find blog widget and then if you want buy a new laptop see the Laptop Price List or you can buy a New Blackberry and then take care your Health & Jewerly good job, very great article Cari Rumah, Jual Rumah, Rumah dijual Thanks ever so much Cari Rumah, Jual Rumah, Rumah dijual very useful article Cari Rumah, Jual Rumah, Rumah dijual Great information Cari Rumah, Jual Rumah, Rumah dijual I like your blog Cari Rumah, Jual Rumah, Rumah dijual I will be checking back for any new articles Cari Rumah, Jual Rumah, Rumah dijual just bookmarked it for later reference Cari Rumah, Jual Rumah, Rumah dijual. Thanks for kindly sharing it with us. Very well done indeed