import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;
public class DemoAddressBookBackup extends Activity {
public static final String TAG = "ADDRESSBOOK BACKUP";
private ToXml toXml;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toXml = new ToXml(Environment.getExternalStorageDirectory().toString()
+ "/test.xml");
populateContacts();
}
private void populateContacts() {
try {
toXml.initXML();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
toXml.startContactElement();
// ID AND NAME FROM CONTACTS CONTRACTS
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i(TAG, "ID :" + id);
Log.i(TAG, "NAME :" + name);
toXml.createContactNameNode(name);
// GET PHONE NUMBERS WITH QUERY STRING
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id },
null);
// WHILE WE HAVE CURSOR GET THE PHONE NUMERS
while (pCur.moveToNext()) {
// Do something with phones
String phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
String phoneType = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
Log.i(TAG, "PHONE :" + phone);
Log.i(TAG, "PHONE TYPE :" + phoneType);
toXml.createPhoneNode(phone, phoneType);
}
pCur.close();
}
// WHILE WE HAVE CURSOR GET THE EMAIL
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
Log.i(TAG, "EMAIL :" + email);
Log.i(TAG, "EMAIL TYPE :" + emailType);
toXml.createEmailNode(email, emailType);
}
emailCur.close();
// FOR GETTTING ZIP & POSTAL
String addrWhere = ContactsContract.Data.CONTACT_ID
+ " = ? AND " + ContactsContract.Data.MIMETYPE
+ " = ?";
String[] addrWhereParams = new String[] {
id,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };
Cursor addrCur = cr.query(
ContactsContract.Data.CONTENT_URI, null, addrWhere,
addrWhereParams, null);
while (addrCur.moveToNext()) {
String poBox = addrCur
.getString(addrCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
String street = addrCur
.getString(addrCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String city = addrCur
.getString(addrCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String state = addrCur
.getString(addrCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String postalCode = addrCur
.getString(addrCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String country = addrCur
.getString(addrCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
String type = addrCur
.getString(addrCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
Log.i(TAG, "POBOX :" + poBox);
Log.i(TAG, "STREET :" + street);
Log.i(TAG, "CITY :" + city);
Log.i(TAG, "STATE :" + state);
Log.i(TAG, "POSTALCODE :" + postalCode);
Log.i(TAG, "COUNTRY :" + country);
Log.i(TAG, "TYPE :" + type);
if (street == null) {
street = "";
}
if (city == null) {
city = "";
}
if (state == null) {
state = "";
}
if (postalCode == null) {
postalCode = "";
}
if (country == null) {
country = "";
}
toXml.createPostalAddressNode(type, street, city,
state, postalCode, country);
}
addrCur.close();
// Get Organization details..
String noteWhere = ContactsContract.Data.CONTACT_ID
+ " = ? AND " + ContactsContract.Data.MIMETYPE
+ " = ?";
String[] noteWhereParams = new String[] {
id,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE };
Cursor noteCur = cr.query(
ContactsContract.Data.CONTENT_URI, null, noteWhere,
noteWhereParams, null);
if (noteCur.moveToFirst()) {
do {
String company = noteCur
.getString(noteCur
.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));
Log.i(TAG, "Org :" + company);
String status = noteCur
.getString(noteCur
.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
Log.i(TAG, "Work :" + status);
toXml.createOrganizationNode(company, status);
} while (noteCur.moveToNext());
}
noteCur.close();
// Instant messanger
String imWhere = ContactsContract.Data.CONTACT_ID
+ " = ? AND " + ContactsContract.Data.MIMETYPE
+ " = ?";
String[] imWhereParams = new String[] {
id,
ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE };
Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI,
null, imWhere, imWhereParams, null);
if (imCur.moveToFirst()) {
String imName = imCur
.getString(imCur
.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
Log.i(TAG, "ImName :" + imName);
String imType;
imType = imCur
.getString(imCur
.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA5));
Log.i(TAG, "ImType :" + imType);
toXml.createIMNode(imName, imType);
}
imCur.close();
// Get Group Details
String groupWhere = ContactsContract.Data.CONTACT_ID
+ " = ? AND " + ContactsContract.Data.MIMETYPE
+ " = ?";
String[] groupWhereParams = new String[] {
id,
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE };
Cursor groupCur = cr.query(
ContactsContract.Data.CONTENT_URI, null,
groupWhere, groupWhereParams, null);
if (groupCur.moveToFirst()) {
do {
String company = groupCur
.getString(groupCur
.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.DATA1));
Log.i(TAG, "Group :" + company);
String status = groupCur
.getString(groupCur
.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID));
Log.i(TAG, "Type :" + status);
} while (groupCur.moveToNext());
}
groupCur.close();
toXml.endContactElement();
}
}
toXml.closeXML();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
ToXml.java
import java.io.BufferedReader;
import java.io.FileReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class ToXml {
public static final String ELE_CONTACTDETAILS = "ContactDetails";
public static final String ELE_CONTACT = "Contact";
public static final String ELE_CONTACT_NAME = "ContactName";
public static final String ELE_PHONE = "Phone";
public static final String ELE_EMAIL = "Email";
public static final String ELE_POSTAL_ADDRESS = "PostalAddress";
public static final String ELE_PHONE_NO = "PhoneNo";
public static final String ELE_TYPE = "Type";
public static final String ELE_MAILID = "MailId";
public static final String ELE_STREET = "Stree";
public static final String ELE_CITY = "City";
public static final String ELE_STATE = "State";
public static final String ELE_PINCODE = "Pincode";
public static final String ELE_COUNTRY = "Country";
public static final String ELE_ORG="Orgazination";
public static final String ELE_COMPANY="Company";
public static final String ELE_STATUS="Status";
public static final String ELE_IM="IM";
public static final String ELE_IMNAME="IMName";
BufferedReader in;
StreamResult out;
TransformerHandler th;
AttributesImpl atts;
private String filePath="";
public ToXml(String path){
this.filePath=path;
out = new StreamResult(filePath);
}
public void doit() {
try {
in = new BufferedReader(new FileReader("C:\\pdf.txt"));
initXML();
String str;
/*while ((str = in.readLine()) != null) {
process(str);
}*/
startContactElement();
createContactNameNode("My Name");
createPhoneNode("90940349034", "work");
createEmailNode("abc@gmail.com", "Home");
createPostalAddressNode("Home", "street-1", "RAjkot", "gujarat", "4848374", "India");
endContactElement();
in.close();
closeXML();
} catch (Exception e) {
e.printStackTrace();
}
}
public void initXML() throws ParserConfigurationException,
TransformerConfigurationException, SAXException {
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory
.newInstance();
th = tf.newTransformerHandler();
Transformer serializer = th.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
serializer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "4");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
th.setResult(out);
th.startDocument();
atts = new AttributesImpl();
th.startElement("", "", ELE_CONTACTDETAILS, atts);
}
public void closeXML() throws SAXException {
th.endElement("", "", ELE_CONTACTDETAILS);
th.endDocument();
}
public void startContactElement() throws SAXException {
atts.clear();
th.startElement("", "", ELE_CONTACT, atts);
}
public void endContactElement() throws SAXException {
atts.clear();
th.endElement("", "", ELE_CONTACT);
}
public void createContactNameNode(String name) throws SAXException {
th.startElement("", "", ELE_CONTACT_NAME, atts);
th.characters(name.toCharArray(), 0, name.length());
th.endElement("", "", ELE_CONTACT_NAME);
}
public void createPhoneNode(String number, String type) throws SAXException {
th.startElement("", "", ELE_PHONE, atts);
th.startElement("", "", ELE_PHONE_NO, atts);
th.characters(number.toCharArray(), 0, number.length());
th.endElement("", "", ELE_PHONE_NO);
th.startElement("", "", ELE_TYPE, atts);
th.characters(type.toCharArray(), 0, type.length());
th.endElement("", "", ELE_TYPE);
th.endElement("", "", ELE_PHONE);
}
public void createEmailNode(String mailid, String type) throws SAXException {
th.startElement("", "", ELE_EMAIL, atts);
th.startElement("", "", ELE_MAILID, atts);
th.characters(mailid.toCharArray(), 0, mailid.length());
th.endElement("", "", ELE_MAILID);
th.startElement("", "", ELE_TYPE, atts);
th.characters(type.toCharArray(), 0, type.length());
th.endElement("", "", ELE_TYPE);
th.endElement("", "", ELE_EMAIL);
}
public void createPostalAddressNode(String type, String street,
String city, String state, String pincode, String country)
throws SAXException {
th.startElement("", "", ELE_POSTAL_ADDRESS, atts);
th.startElement("", "", ELE_TYPE, atts);
th.characters(type.toCharArray(), 0, type.length());
th.endElement("", "", ELE_TYPE);
th.startElement("", "", ELE_STREET, atts);
th.characters(street.toCharArray(), 0, street.length());
th.endElement("", "", ELE_STREET);
th.startElement("", "", ELE_CITY, atts);
th.characters(city.toCharArray(), 0, city.length());
th.endElement("", "", ELE_CITY);
th.startElement("", "", ELE_STATE, atts);
th.characters(state.toCharArray(), 0, state.length());
th.endElement("", "", ELE_STATE);
th.startElement("", "", ELE_PINCODE, atts);
th.characters(pincode.toCharArray(), 0, pincode.length());
th.endElement("", "", ELE_PINCODE);
th.startElement("", "", ELE_COUNTRY, atts);
th.characters(country.toCharArray(), 0, country.length());
th.endElement("", "", ELE_COUNTRY);
th.endElement("", "", ELE_POSTAL_ADDRESS);
}
public void createOrganizationNode(String company, String status) throws SAXException {
th.startElement("", "", ELE_ORG, atts);
th.startElement("", "", ELE_COMPANY, atts);
th.characters(company.toCharArray(), 0, company.length());
th.endElement("", "", ELE_COMPANY);
th.startElement("", "", ELE_STATUS, atts);
th.characters(status.toCharArray(), 0, status.length());
th.endElement("", "", ELE_STATUS);
th.endElement("", "", ELE_ORG);
}
public void createIMNode(String ImName, String type) throws SAXException {
th.startElement("", "", ELE_IM, atts);
th.startElement("", "", ELE_IMNAME, atts);
th.characters(ImName.toCharArray(), 0,ImName.length());
th.endElement("", "", ELE_IMNAME);
th.startElement("", "", ELE_TYPE, atts);
th.characters(type.toCharArray(), 0, type.length());
th.endElement("", "", ELE_TYPE);
th.endElement("", "", ELE_IM);
}
}