title: How to save nodes and values to an xml file through java /Android Studio
body: <p>The application when run currently has two TextViews, two TextEdits, with a save button which saves the password and email to String variables. I have an xml file called users.xml to which I would like to save the email and password when the button is pressed(in addition to saving the email and password to String variables) 
  Any suggestions? </p>
  
  <p>Code:</p>
  
  <pre><code>package com.example.ashwinpraveen1.domdoc;
  
  import android.content.res.AssetManager;
  import android.os.Bundle;
  import android.support.v7.app.ActionBarActivity;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.NodeList;
  import org.xml.sax.InputSource;
  
  import java.io.InputStream;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  
  
  public class MainActivity extends ActionBarActivity {
  
      TextView printText;
      EditText emailEdit, passwordEdit;
      Button saveButton;
      String email,password;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          final EditText emailEdit = (EditText) findViewById(R.id.emailEdit);
          final EditText passwordEdit = (EditText) findViewById(R.id.passwordEdit);
          Button saveButton = (Button) findViewById(R.id.saveButton);
  
          Document xmlDoc = getDocument();
          final TextView printText = (TextView) findViewById(R.id.printText);
          // checking if I can retrieve the root node
          printText.setText(xmlDoc.getDocumentElement().getNodeName());
  
  
          saveButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  email = emailEdit.getText().toString();
                  password = passwordEdit.getText().toString();
                  if (email.isEmpty() || password.isEmpty()) {
                      //code when the user presses the "save" button for no reason
                  }
              }
          });
  
  
  
  
              }
  
              private  Document getDocument() {
                  try {
                      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                      factory.setIgnoringComments(true);
                      factory.setIgnoringElementContentWhitespace(true);
                      //factory.setValidating(true);
  
                      DocumentBuilder builder = factory.newDocumentBuilder();
                      AssetManager assetManager = this.getAssets();
                      InputStream is = assetManager.open("users.xml");
  
                      InputSource inStream = new InputSource(is);
                      return builder.parse(inStream);
  
                  }
                  catch(Exception e) {
                      TextView printText = (TextView) findViewById(R.id.printText);
                      printText.setText(e.getMessage());
                      return null;
                  }
              }
  
  
  
      }
  </code></pre>
  
  <p>users.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;list&gt;
      &lt;users&gt;
          &lt;user1&gt;Bob&lt;/user1&gt;
          &lt;pass1&gt;BobNeedsHelpWithxml&lt;/pass1&gt;
      &lt;/users&gt;
      &lt;users&gt;
          &lt;user2&gt;Ash&lt;/user2&gt;
          &lt;pass2&gt;AshNeedsHelpToo&lt;/pass2&gt;
      &lt;/users&gt;
  &lt;/list&gt;
  </code></pre>