Sunday, 7 December 2014

how to insert with xml



i am trying to save an xml data in sqlite database. am i doing it right i can display it normaly but i can't save it. please it you think my code wont work . try to suggest a way out. because i an stuck at this point.



package com.newthinktank.contactsapp;

import java.util.HashMap;

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.os.Bundle;

import android.view.View;

import android.widget.EditText;


public class NewContact extends Activity{

static final String URL = "http://ift.tt/1ys7F8d";

EditText firstName;
EditText lastName;
EditText phoneNumber;
EditText emailAddress;
EditText homeAddress;

static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_AGE = "age";
static final String KEY_SCHOOL = "school";

DBTools dbTools = new DBTools(this);

@Override
public void onCreate(Bundle savedInstanceState) {

// Get saved data if there is any

super.onCreate(savedInstanceState);

// Designate that add_new_contact.xml is the interface used

setContentView(R.layout.add_new_contact);

// Initialize the EditText objects

firstName = (EditText) findViewById(R.id.firstName);
lastName = (EditText) findViewById(R.id.lastName);
phoneNumber = (EditText) findViewById(R.id.phoneNumber);
emailAddress = (EditText) findViewById(R.id.emailAddress);
homeAddress = (EditText) findViewById(R.id.homeAddress);

}
public void addNewContact(View view) {

Saxml parser = new Saxml();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap

HashMap<String, String> queryValuesMap = new HashMap<String, String>();

Element e = (Element) nl.item(i);

queryValuesMap.put("firstName", parser.getValue(e, KEY_ID));
queryValuesMap.put("lastName", parser.getValue(e, KEY_NAME));
queryValuesMap.put("pnoneNumber", parser.getValue(e, KEY_ID));
queryValuesMap.put("emailAddress", parser.getValue(e, KEY_SCHOOL));
queryValuesMap.put("homeAddress", parser.getValue(e, KEY_AGE));

// Call for the HashMap to be added to the database

dbTools.insertContact(queryValuesMap);

// Call for MainActivity to execute

this.callMainActivity(view);
}
}
public void callMainActivity(View view) {
Intent theIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(theIntent);
}
}


this is my xml data



<?xml version="1.0" encoding="utf-8"?>
<menu>
<item>
<id>1</id>
<name>obi</name>
<age>27</age>
<school> madonna</school>
</item>
<item>


and this is my dbtool



package com.newthinktank.contactsapp;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBTools extends SQLiteOpenHelper {

public DBTools(Context applicationContext){

super(applicationContext, "contactbook.db", null, 1);

}

@Override
public void onCreate(SQLiteDatabase database) {

String query = "CREATE TABLE contacts ( contactId INTEGER PRIMARY KEY, firstName TEXT, " +
"lastName TEXT, phoneNumber TEXT, emailAddress TEXT, homeAddress TEXT)";

database.execSQL(query);

}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {

String query = "DROP TABLE IF EXISTS contacts";

database.execSQL(query);
onCreate(database);

}

public void insertContact(HashMap<String, String> queryValues){

SQLiteDatabase database = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put("firstName", queryValues.get("firstName"));
values.put("lastName", queryValues.get("lastName"));
values.put("phoneNumber", queryValues.get("phoneNumber"));
values.put("emailAddress", queryValues.get("emailAddress"));
values.put("homeAddress", queryValues.get("homeAddress"));

database.insert("contacts", null, values);

database.close();

}

public int updateContact(HashMap<String, String> queryValues){

SQLiteDatabase database = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put("firstName", queryValues.get("firstName"));
values.put("lastName", queryValues.get("lastName"));
values.put("phoneNumber", queryValues.get("phoneNumber"));
values.put("emailAddress", queryValues.get("emailAddress"));
values.put("homeAddress", queryValues.get("homeAddress"));

return database.update("contacts", values,
"contactId" + " = ?", new String[] {queryValues.get("contactId") });

}

public void deleteContact(String id){

SQLiteDatabase database = this.getWritableDatabase();

String deleteQuery = "DELETE FROM contacts WHERE contactId='" + id + "'";

database.execSQL(deleteQuery);

}

public ArrayList<HashMap<String, String>> getAllContacts(){

ArrayList<HashMap<String, String>> contactArrayList = new ArrayList<HashMap<String, String>>();

String selectQuery = "SELECT * FROM contacts ORDER BY lastName";

SQLiteDatabase database = this.getWritableDatabase();

Cursor cursor = database.rawQuery(selectQuery, null);

if(cursor.moveToFirst()){

do{

HashMap<String, String> contactMap = new HashMap<String, String>();

contactMap.put("contactId", cursor.getString(0));
contactMap.put("firstName", cursor.getString(1));
contactMap.put("lastName", cursor.getString(2));
contactMap.put("phoneNumber", cursor.getString(3));
contactMap.put("emailAddress", cursor.getString(4));
contactMap.put("homeAddress", cursor.getString(5));

contactArrayList.add(contactMap);

} while(cursor.moveToNext());

}

return contactArrayList;

}

public HashMap<String, String> getContactInfo(String id){

HashMap<String, String> contactMap = new HashMap<String, String>();

SQLiteDatabase database = this.getReadableDatabase();

String selectQuery = "SELECT * FROM contacts WHERE contactId='" + id + "'";

Cursor cursor = database.rawQuery(selectQuery, null);

if(cursor.moveToFirst()){

do{

contactMap.put("contactId", cursor.getString(0));
contactMap.put("firstName", cursor.getString(1));
contactMap.put("lastName", cursor.getString(2));
contactMap.put("phoneNumber", cursor.getString(3));
contactMap.put("emailAddress", cursor.getString(4));
contactMap.put("homeAddress", cursor.getString(5));


} while(cursor.moveToNext());

}

return contactMap;

}

}

No comments:

Post a Comment