• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Add Node to Existing Xml Document

InkedAndroid

Newbie
Dec 12, 2015
10
2
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....

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!
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones