1. Are you ready for the Galaxy S20? Here is everything we know so far!

Add Node to Existing Xml Document

Discussion in 'Android Apps & Games' started by InkedAndroid, Jan 30, 2016.

  1. InkedAndroid

    InkedAndroid Newbie
    Thread Starter

    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 (Text):
    1. try {
    2.  
    3.                               DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    4.                               DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    5.                               Document doc = docBuilder.parse(new FileInputStream(new File(path)));
    6.  
    7.                               //Root Node [Customers]
    8.                               Element root =  doc.getDocumentElement();
    9.  
    10.                               //Create New Customer Node
    11.                               Element newCust = doc.createElement("Customer");
    12.  
    13.                               //Create New Customer Name Node
    14.                               Element customerName = doc.createElement("Name");
    15.                               customerName.appendChild(doc.createTextNode(custName.getText().toString()));
    16.                               newCust.appendChild(customerName);
    17.  
    18.                               //Create New Customer Address Node
    19.                               Element customerAddress = doc.createElement("Address");
    20.                               customerAddress.appendChild(doc.createTextNode(custAddress.getText().toString()));
    21.                               newCust.appendChild(customerAddress);
    22.  
    23.                               //Create New Customer Phone Node
    24.                               Element customerPhone = doc.createElement("Phone");
    25.                               customerPhone.appendChild(doc.createTextNode(custPhone.getText().toString()));
    26.                               newCust.appendChild(customerPhone);
    27.  
    28.                               //Create New Customer Email
    29.                               Element customerEmail = doc.createElement("Email");
    30.                               customerEmail.appendChild(doc.createTextNode(custEmail.getText().toString()));
    31.                               newCust.appendChild(customerEmail);
    32.  
    33.                               //Write the New Customer Info to the Xml
    34.                               TransformerFactory transformerFactory = TransformerFactory.newInstance();
    35.                               Transformer transformer = transformerFactory.newTransformer();
    36.                               DOMSource domSource = new DOMSource(doc);
    37.  
    38.                               StreamResult streamResult = new StreamResult(new FileOutputStream(path));
    39.                               transformer.transform(domSource, streamResult);
    40.  
    41.                               Toast.makeText(Create_Customer_Activity.this, "XML Updated Successfully!", Toast.LENGTH_LONG).show();
    42.                           }
    43.                           catch (IOException e) {
    44.                             Log.e("EstimaTEE", "IO Error - " + e.getMessage());
    45.                           }
    46.                           catch (ParserConfigurationException pe) {
    47.                               Log.e("EstimaTEE", "Parse Error - " + pe.getMessage());
    48.                           }
    49.                           catch (TransformerException te) {
    50.                               Log.e("EstimaTEE", "Transform Error - " + te.getMessage());
    51.                           }
    52.                           catch (SAXException se) {
    53.                               Log.e("EstimaTEE", "SAX Error - " + se.getMessage());
    54.                           }
    any help is much appreciated!
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...