Hope this will help you..:)
Here two class DOMParsingTestingActivity and XMLParser are used..
Class:DOMParsingTestingActivity
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class DOMParsingTestingActivity extends Activity
{
String deviceid;
String mailing_address_value;
String phone_number_value;
String first_name_value;
String last_name_value;
String email_address_value;
boolean show=false;
String response = null;
String url2;
String email_address_edit;
String lastname_edit;
String phone_number_edit;
String result1;
String first_name_edit;
Button savebuttton,backbton;
EditText first_name;
EditText last_name;
EditText email_address;
EditText phone_number;
EditText mailing_address;
String mailing_address_edit;
String KEY_Result = "Result";
String KEY_love_rnr = "LoveRnR";
String xml;
String id;
String urlshow;
SharedPreferences mypref;
String KEY_Userdetail="UsersDetails";
String KEY_Firstname="FirstName";
String KEY_Lastname="LastName";
String KEY_Emailaddress="EmailAddress";
String KEY_Mailingaddress="MailingAddress";
String KEY_Phonenumber="PhoneNumber";
ArrayList<String> user_detail_list;
ArrayList<String> first_name_list;
ArrayList<String> last_name_list;
ArrayList<String> email_address_list;
ArrayList<String> phone_number_list;
ArrayList<String> mailing_address_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
deviceid=android.provider.Settings.System.getString(DOMParsingTestingActivity.this.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
mypref = PreferenceManager.getDefaultSharedPreferences(this);
first_name=(EditText)findViewById(R.id.first_nameeditbox);
last_name=(EditText)findViewById(R.id.last_nameeditbox);
email_address=(EditText)findViewById(R.id.email_addeditbox);
phone_number=(EditText)findViewById(R.id.phone_number_editprofile);
mailing_address=(EditText)findViewById(R.id.mailing_addeditbox);
savebuttton=(Button)findViewById(R.id.savebtnid);
backbton=(Button)findViewById(R.id.backbtnid);
//id=mypref.getString("Userid", "");
//static value response
id=String.valueOf(6);
user_detail_list=new ArrayList<String>();
first_name_list=new ArrayList<String>();
last_name_list=new ArrayList<String>();
email_address_list=new ArrayList<String>();
phone_number_list=new ArrayList<String>();
mailing_address_list=new ArrayList<String>();
urlshow="Enter your url /showdetails.php?user_id="+id;
XMLParser parser=new XMLParser();
String xml = parser.getXmlFromUrl(urlshow);
Log.e("SplashScreen=",""+xml);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_Userdetail);
for (int i=0; i<nl.getLength();i++)
{
Element e = (Element) nl.item(i);
user_detail_list.add(parser.getValue(e, KEY_Userdetail));
first_name_list.add(parser.getValue(e, KEY_Firstname));
last_name_list.add(parser.getValue(e, KEY_Lastname));
email_address_list.add(parser.getValue(e, KEY_Emailaddress));
phone_number_list.add(parser.getValue(e,KEY_Phonenumber));
mailing_address_list.add(parser.getValue(e,KEY_Mailingaddress));
first_name_value=parser.getValue(e, KEY_Firstname).toString().trim();
last_name_value=parser.getValue(e, KEY_Lastname).toString().trim();
email_address_value=parser.getValue(e, KEY_Emailaddress).toString().trim();
mailing_address_value=parser.getValue(e, KEY_Mailingaddress).toString().trim();
phone_number_value=parser.getValue(e, KEY_Phonenumber).toString().trim();
first_name.setText(String.valueOf(first_name_value));
last_name.setText(String.valueOf(last_name_value));
email_address.setText(String.valueOf(email_address_value));
phone_number.setText(String.valueOf(phone_number_value));
mailing_address.setText(String.valueOf(mailing_address_value));
Log.e("",""+first_name_value);
Log.e("",""+last_name_value);
}
savebuttton.setOnClickListener(new OnClickListener()
{
public void onClick(View paramView)
{
save();
}
});
backbton.setOnClickListener(new OnClickListener() {
public void onClick(View paramView)
{
Intent in=new Intent(DOMParsingTestingActivity.this,DOMParsingTestingActivity.class);
startActivity(in);
}
});
}
public void save()
{
first_name_edit=first_name.getEditableText().toString().trim();
lastname_edit=last_name.getEditableText().toString().trim();
email_address_edit=email_address.getEditableText().toString().trim();
phone_number_edit=phone_number.getEditableText().toString().trim();
mailing_address_edit=mailing_address.getEditableText().toString().trim();
if(first_name_edit.equalsIgnoreCase("")|| lastname_edit.equalsIgnoreCase("")|| email_address_edit.equalsIgnoreCase("")|| phone_number_edit.equalsIgnoreCase("")||mailing_address_edit.equalsIgnoreCase(""))
{
Toast.makeText(DOMParsingTestingActivity.this,"Please enter all the fields",Toast.LENGTH_LONG).show();
}
else
{
boolean isValidEmail = Util.isEmailValid(email_address_edit);
boolean callMethod = true;
if(isValidEmail == false)
{
callMethod = false;
Toast.makeText(DOMParsingTestingActivity.this, "Please enter a valid email address.", Toast.LENGTH_LONG).show();
}
if(phone_number_edit.length()<10)
{
callMethod = false;
Toast.makeText(DOMParsingTestingActivity.this, "Please enter valid phone number",Toast.LENGTH_LONG).show();
}
if(callMethod)
{
url2="Enter your url /updateuser.php?user_id="+id+"&last_name="+lastname_edit+"&first_name="+first_name_edit+"&email_address="+email_address_edit+"&phone_number="+phone_number_edit+"&mailing_address="+mailing_address_edit+"&token_id="+deviceid;
HttpURLConnection connection;
OutputStreamWriter request = null;
try
{
URL url = new URL(url2);
Log.i("URL", "url is "+url.toString());
Log.e("",""+url);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
request = new OutputStreamWriter(connection.getOutputStream());
request.write(url2);
request.flush();
request.close();
String line = "";
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
response = sb.toString();
Log.e("ResponseRelation=",""+response);
Toast.makeText(DOMParsingTestingActivity.this,"Saved sucessfully.",Toast.LENGTH_LONG).show();
isr.close();
reader.close();
}
catch(IOException e)
{
}
}
}
}
}
Class:XMLParser
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.util.Log;
public class XMLParser
{
String xml = null;
Document doc = null;
/**
* Getting XML from URL making HTTP request
* @param url string
* */
public String getXmlFromUrl(String url)
{
try
{
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* @param XML string
* */
public Document getDomElement(String xml)
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e)
{
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e)
{
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e)
{
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
public String getValue(Element item, String str)
{
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() )
{
if( child.getNodeType() == Node.TEXT_NODE )
{
return child.getNodeValue();
}
}
}
}
return "";
}
}