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: