I am trying to save customer data to xml in the Android internal storage...I use the xml serializer to create the initial xml.. this works....now I need to add nodes to the xml document. I am trying to use the DOM Parser.
below is the code I have to add the new node and the node information....
any help is much appreciated!
below is the code I have to add the new node and the node information....
Code:
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new FileInputStream(new File(path)));
//Root Node [Customers]
Element root = doc.getDocumentElement();
//Create New Customer Node
Element newCust = doc.createElement("Customer");
//Create New Customer Name Node
Element customerName = doc.createElement("Name");
customerName.appendChild(doc.createTextNode(custName.getText().toString()));
newCust.appendChild(customerName);
//Create New Customer Address Node
Element customerAddress = doc.createElement("Address");
customerAddress.appendChild(doc.createTextNode(custAddress.getText().toString()));
newCust.appendChild(customerAddress);
//Create New Customer Phone Node
Element customerPhone = doc.createElement("Phone");
customerPhone.appendChild(doc.createTextNode(custPhone.getText().toString()));
newCust.appendChild(customerPhone);
//Create New Customer Email
Element customerEmail = doc.createElement("Email");
customerEmail.appendChild(doc.createTextNode(custEmail.getText().toString()));
newCust.appendChild(customerEmail);
//Write the New Customer Info to the Xml
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(doc);
StreamResult streamResult = new StreamResult(new FileOutputStream(path));
transformer.transform(domSource, streamResult);
Toast.makeText(Create_Customer_Activity.this, "XML Updated Successfully!", Toast.LENGTH_LONG).show();
}
catch (IOException e) {
Log.e("EstimaTEE", "IO Error - " + e.getMessage());
}
catch (ParserConfigurationException pe) {
Log.e("EstimaTEE", "Parse Error - " + pe.getMessage());
}
catch (TransformerException te) {
Log.e("EstimaTEE", "Transform Error - " + te.getMessage());
}
catch (SAXException se) {
Log.e("EstimaTEE", "SAX Error - " + se.getMessage());
}
any help is much appreciated!