我正在解析在不同元素下具有相同标签的 XML 我想解析这个 XML 并保存到相应的 Hash-Map 中,然后保存到数据库中。我在解析具有我想要解析的常见 XML 字段的元素时遇到问题是
<NewDataSet>
<Login>
<LoginId>14</LoginId>
<CompanyId>2</CompanyId>
<UserId>14</UserId>
<LoginName>deo5</LoginName>
<UserPassword>Y2NzYTIwMTI=</UserPassword>
<CreatedBy>14</CreatedBy>
<CreatedDate>2012-04-19T17:54:57.013+05:30</CreatedDate>
<LastLogin>2012-04-19T17:54:57.013+05:30</LastLogin>
<Status>1</Status>
<Approved_Status>true</Approved_Status>
</Login>
<Login>
<LoginId>15</LoginId>
<CompanyId>2</CompanyId>
<UserId>15</UserId>
<LoginName>Philip</LoginName>
<UserPassword>cGhpbGlwMTIz</UserPassword>
<CreatedBy>15</CreatedBy>
<CreatedDate>2012-05-08T09:02:40.53+05:30</CreatedDate>
<LastLogin>2012-05-08T09:02:40.53+05:30</LastLogin>
<Status>1</Status>
<Approved_Status>true</Approved_Status>
</Login>
<Login>
<LoginId>16</LoginId>
<CompanyId>2</CompanyId>
<UserId>16</UserId>
<LoginName>sagar</LoginName>
<UserPassword>c2FnYXIyMDEy</UserPassword>
<CreatedBy>16</CreatedBy>
<CreatedDate>2012-11-27T17:57:11.433+05:30</CreatedDate>
<LastLogin>2012-11-27T17:57:11.433+05:30</LastLogin>
<Status>1</Status>
<Approved_Status>true</Approved_Status>
</Login>
<ProgramFamerTable>
<ProgramFarmer_ID>1</ProgramFarmer_ID>
<Program_ID>7</Program_ID>
<Farmer_Code>INGUJUMAKO-2012-001</Farmer_Code>
<Company_ID>2</Company_ID>
<Organization_ID>1</Organization_ID>
<Language_ID>1</Language_ID>
<Active_Status>true</Active_Status>
<Created_By>1</Created_By>
<Created_Date>2012-04-24T10:30:53.823+05:30</Created_Date>
<Delete_Status>false</Delete_Status>
<Approved_Status>true</Approved_Status>
</ProgramFamerTable>
<ProgramFamerTable>
<ProgramFarmer_ID>2</ProgramFarmer_ID>
<Program_ID>7</Program_ID>
<Farmer_Code>INGUJUMAKO-2012-002</Farmer_Code>
<Company_ID>2</Company_ID>
<Organization_ID>1</Organization_ID>
<Language_ID>1</Language_ID>
<Active_Status>true</Active_Status>
<Created_By>1</Created_By>
<Created_Date>2012-04-24T10:30:53.833+05:30</Created_Date>
<Delete_Status>false</Delete_Status>
<Approved_Status>true</Approved_Status>
</ProgramFamerTable>
<ProgramFamerTable>
<ProgramFarmer_ID>3</ProgramFarmer_ID>
<Program_ID>7</Program_ID>
<Farmer_Code>INGUJUMAKO-2012-003</Farmer_Code>
<Company_ID>2</Company_ID>
<Organization_ID>1</Organization_ID>
<Language_ID>1</Language_ID>
<Active_Status>true</Active_Status>
<Created_By>1</Created_By>
<Created_Date>2012-04-24T10:30:53.837+05:30</Created_Date>
<Delete_Status>false</Delete_Status>
<Approved_Status>true</Approved_Status>
</ProgramFamerTable>
</NewDataSet>
我的 XML 处理程序如下...
public class NewMyXMLHandler extends DefaultHandler
{
String TAG = "Inside Login Element";
//XML Fields of login file
boolean in_Loginmain;
boolean in_Login;
boolean in_LoginId;
boolean in_CompanyId;
boolean in_UserId;
boolean in_LoginName;
boolean in_UserPassword;
boolean in_CreatedBy;
boolean in_CreatedDate;
boolean in_UpdatedBy;
boolean in_LastLogin;
boolean in_Status;
//For Database Insertion
private static String TABLE_NAME = "AD_Login";
private static String TABLE_NAME1 = "AD_ProgramFarmer";
private SQLiteDatabase myDataBase;
boolean in_ProgramFamerTable;
boolean in_ProgramFarmer_ID;
boolean in_Program_ID;
boolean in_Farmer_Code;
boolean in_Company_ID;
boolean in_Organization_ID;
boolean in_Language_ID;
boolean in_Active_Status;
boolean in_Created_By;
boolean in_Created_Date;
boolean in_Delete_Status;
boolean in_Approved_Status;
HashMap<String,String> map;
//public static ArrayList<HashMap<String,String>> list;
//For Farmer Registration
HashMap<String,String> farmerMap;
//public static ArrayList<HashMap<String,String>> faremerlist;
public NewMyXMLHandler(Context applicationContext) {
// TODO Auto-generated constructor stub
try{
//myDataBase.beginTransaction();
DatabaseHelper dh = new DatabaseHelper(applicationContext);
dh.createDataBase();
dh.openDataBase();
myDataBase= dh.getWritableDatabase();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException
{
// TODO Auto-generated method stub
super.characters(ch, start, length);
String data = new String(ch, start, length);
if (in_LoginId)
{
map.put("LoginId", data);
Log.i("", "contactid = " + data);
}
if (in_CompanyId)
{
map.put("CompanyId", data);
Log.i("", "contact_firstname = " + data);
}
if (in_UserId)
{
map.put("UserId", data);
Log.i("", "contact_lastname = " + data);
}
if (in_LoginName)
{
map.put("LoginName", data);
Log.i("", "contact_email = " + data);
}
if (in_UserPassword)
{
map.put("UserPassword", data);
Log.i("", "contact_thumbnail = " + data);
}
if (in_CreatedBy)
{
map.put("CreatedBy", data);
Log.i("", "contact_thumbnail = " + data);
}
if (in_CreatedDate)
{
map.put("CreatedDate", data);
Log.i("", "contact_thumbnail = " + data);
}
if (in_UpdatedBy)
{
map.put("UpdatedBy", data);
Log.i("", "contact_thumbnail = " + data);
}
if (in_LastLogin)
{
map.put("LastLogin", data);
Log.i("", "contact_thumbnail = " + data);
}
if (in_Status)
{
map.put("Status", data);
Log.i("", "contact_thumbnail = " + data);
}
if (in_Approved_Status)
{
map.put("Approved_Status", data);
Log.i("", "Approved_Status = " + data);
}
//Program Farmer Started Here......
if (in_ProgramFarmer_ID)
{
farmerMap.put("ProgramFarmer_ID", data);
Log.i("", "ProgramFarmer_ID = " + data);
}
if (in_Program_ID)
{
farmerMap.put("Program_ID", data);
Log.i("", "Program_ID = " + data);
}
if (in_Farmer_Code)
{
farmerMap.put("Farmer_Code", data);
Log.i("", "Farmer_Code = " + data);
}
if (in_Company_ID)
{
farmerMap.put("Company_ID", data);
Log.i("", "Company_ID = " + data);
}
if (in_Organization_ID)
{
farmerMap.put("Organization_ID", data);
Log.i("", "Organization_ID = " + data);
}
if (in_Language_ID)
{
farmerMap.put("Language_ID", data);
Log.i("", "Language_ID = " + data);
}
if (in_Active_Status)
{
farmerMap.put("Active_Status", data);
Log.i("", "Active_Status = " + data);
}
if (in_Created_By)
{
farmerMap.put("Created_By", data);
Log.i("", "Created_By = " + data);
}
if (in_Created_Date)
{
farmerMap.put("Created_Date", data);
Log.i("", "Created_Date = " + data);
}
if (in_Delete_Status)
{
farmerMap.put("Delete_Status", data);
Log.i("", "Delete_Status = " + data);
}
/*if (in_Approved_Status)
{
farmerMap.put("Approved_Status", data);
Log.i("", "Approved_Status = " + data);
}*/
}
@Override
public void endDocument() throws SAXException
{
// TODO Auto-generated method stub
super.endDocument();
Log.i("", "endDocument");
myDataBase.close();
//myDataBase.endTransaction();
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
Log.i("", "endElement");
if (localName.equals("Loginmain"))
{
in_Loginmain = false;
} else if (localName.equals("Login"))
{
in_Login = false;
//list.add(map);
Log.v(TAG, "Value of Login Element=="+map);
//insertLoginData();
} else if (localName.equals("LoginId"))
{
in_LoginId = false;
//list.add(map);
} else if (localName.equals("CompanyId"))
{
in_CompanyId = false;
} else if (localName.equals("UserId"))
{
in_UserId = false;
} else if (localName.equals("LoginName"))
{
in_LoginName = false;
} else if (localName.equals("UserPassword"))
{
in_UserPassword = false;
} else if (localName.equals("CreatedBy"))
{
in_CreatedBy = false;
}else if (localName.equals("CreatedDate"))
{
in_CreatedDate = false;
}else if (localName.equals("UpdatedBy"))
{
in_UpdatedBy = false;
}
else if (localName.equals("LastLogin"))
{
in_LastLogin = false;
}else if (localName.equals("Status"))
{
in_Status = false;
}else if (localName.equals("Approved_Status"))
{
in_Approved_Status = false;
}//Farmer Program.........
else if (localName.equals("ProgramFamerTable"))
{
in_ProgramFamerTable = false;
Log.v(TAG, "Values of ProgramFarmerMap==="+farmerMap);
//insertProgramFarmerData();
} else if (localName.equals("ProgramFarmer_ID"))
{
in_ProgramFarmer_ID = false;
//list.add(map);
} else if (localName.equals("Program_ID"))
{
in_Program_ID = false;
} else if (localName.equals("Farmer_Code"))
{
in_Farmer_Code = false;
} else if (localName.equals("Company_ID"))
{
in_Company_ID = false;
} else if (localName.equals("Organization_ID"))
{
in_Organization_ID = false;
} else if (localName.equals("Language_ID"))
{
in_Language_ID = false;
}else if (localName.equals("Active_Status"))
{
in_Active_Status = false;
}else if (localName.equals("Created_By"))
{
in_Created_By = false;
}
else if (localName.equals("Created_Date"))
{
in_Created_Date = false;
}else if (localName.equals("Delete_Status"))
{
in_Delete_Status = false;
}else if (localName.equals("Approved_Status"))
{
in_Approved_Status = false;
}
}
private void insertProgramFarmerData() {
// TODO Auto-generated method stub
try{
ContentValues cvprogramfarmer = new ContentValues();
if (farmerMap.get("ProgramFarmer_ID") != null)
{
cvprogramfarmer.put("ProgramFarmer_ID",farmerMap.get("ProgramFarmer_ID").toString());
}
if (farmerMap.get("Program_ID") != null)
{
cvprogramfarmer.put("Program_ID", farmerMap.get("Program_ID").toString());
}
if (farmerMap.get("Farmer_Code") != null)
{
cvprogramfarmer.put("Farmer_Code", farmerMap.get("Farmer_Code").toString());
}
if (farmerMap.get("Company_ID") != null)
{
cvprogramfarmer.put("Company_ID", farmerMap.get("Company_ID").toString());
}
if (farmerMap.get("Organization_ID") != null)
{
cvprogramfarmer.put("Organization_ID", farmerMap.get("Organization_ID").toString());
}if (farmerMap.get("Language_ID") != null)
{
cvprogramfarmer.put("Language_ID", farmerMap.get("Language_ID").toString());
}
if (farmerMap.get("Active_Status") != null)
{
cvprogramfarmer.put("Active_Status", farmerMap.get("Active_Status").toString());
}
if (farmerMap.get("Created_By") != null)
{
cvprogramfarmer.put("Created_By", farmerMap.get("Created_By").toString());
}
if (farmerMap.get("Delete_Status") != null)
{
cvprogramfarmer.put("Delete_Status", farmerMap.get("Delete_Status").toString());
}
if (farmerMap.get("Created_Date") != null)
{
cvprogramfarmer.put("Created_Date", farmerMap.get("Created_Date").toString());
}
if (farmerMap.get("Approved_Status") != null)
{
cvprogramfarmer.put("Approved_Status", farmerMap.get("Approved_Status").toString());
}
Log.v(TAG, "Content Values=="+cvprogramfarmer);
//myDataBase.execSQL("SELECT * FROM AD_Login");
Log.v(TAG, "Values are inserting");
myDataBase.insert(TABLE_NAME1, null,cvprogramfarmer);
Log.v(TAG, "Values are inserted");
//cvlogin=null;
Log.v(TAG, "Values after NULL "+cvprogramfarmer);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
private void insertLoginData() {
// TODO Auto-generated method stub
try{
ContentValues cvlogin = new ContentValues();
if (map.get("LoginId") != null)
{
cvlogin.put("LoginId", map.get("LoginId").toString());
}
if (map.get("CompanyId") != null)
{
cvlogin.put("CompanyId", map.get("CompanyId").toString());
}
if (map.get("UserId") != null)
{
cvlogin.put("UserId", map.get("UserId").toString());
}
if (map.get("LoginName") != null)
{
cvlogin.put("LoginName", map.get("LoginName").toString());
}
if (map.get("UserPassword") != null)
{
cvlogin.put("UserPassword", map.get("UserPassword").toString());
}
/*if (map.get("CreatedBy") != null)
{
cvlogin.put("CreatedBy", map.get("CreatedBy").toString());
}
if (map.get("CreatedDate") != null)
{
cvlogin.put("CreatedDate", map.get("CreatedDate").toString());
}
if (map.get("UpdatedBy") != null)
{
cvlogin.put("UpdatedBy", map.get("UpdatedBy").toString());
}*/
if (map.get("LastLogin") != null)
{
cvlogin.put("LastLogin", map.get("LastLogin").toString());
}
if (map.get("Status") != null)
{
cvlogin.put("Status", map.get("Status").toString());
}
Log.v(TAG, "Values"+cvlogin);
//myDataBase.execSQL("SELECT * FROM AD_Login");
Log.v(TAG, "Values are inserting");
myDataBase.insert(TABLE_NAME, null,cvlogin);
Log.v(TAG, "Values are inserted");
//cvlogin=null;
Log.v(TAG, "Values after NULL "+cvlogin);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public void startDocument() throws SAXException
{
// TODO Auto-generated method stub
super.startDocument();
Log.i("", "startDocument");
//list = new ArrayList<HashMap<String,String>>();
//faremerlist = new ArrayList<HashMap<String,String>>();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
Log.i("", "startElement");
if (localName.equals("Loginmain"))
{
in_Loginmain = true;
} else if (localName.equals("Login"))
{
in_Login = true;
map = new HashMap<String, String>();
} else if (localName.equals("LoginId"))
{
in_LoginId = true;
//list.add(map);
} else if (localName.equals("CompanyId"))
{
in_CompanyId = true;
} else if (localName.equals("UserId"))
{
in_UserId = true;
} else if (localName.equals("LoginName"))
{
in_LoginName = true;
} else if (localName.equals("UserPassword"))
{
in_UserPassword = true;
} else if (localName.equals("CreatedBy"))
{
in_CreatedBy = true;
}else if (localName.equals("CreatedDate"))
{
in_CreatedDate = true;
}else if (localName.equals("UpdatedBy"))
{
in_UpdatedBy = true;
}
else if (localName.equals("LastLogin"))
{
in_LastLogin = true;
}else if (localName.equals("Status"))
{
in_Status = true;
}
else if (localName.equals("Approved_Status"))
{
in_Approved_Status = true;
}//Farmer Program............
else if (localName.equals("ProgramFamerTable"))
{
in_ProgramFamerTable = true;
farmerMap = new HashMap<String, String>();
} else if (localName.equals("ProgramFarmer_ID"))
{
in_ProgramFarmer_ID = true;
//list.add(map);
} else if (localName.equals("Program_ID"))
{
in_Program_ID = true;
} else if (localName.equals("Farmer_Code"))
{
in_Farmer_Code = true;
} else if (localName.equals("Company_ID"))
{
in_Company_ID = true;
} else if (localName.equals("Organization_ID"))
{
in_Organization_ID = true;
} else if (localName.equals("Language_ID"))
{
in_Language_ID = true;
}else if (localName.equals("Active_Status"))
{
in_Active_Status = true;
}else if (localName.equals("Created_By"))
{
in_Created_By = true;
}
else if (localName.equals("Created_Date"))
{
in_Created_Date = true;
}else if (localName.equals("Delete_Status"))
{
in_Delete_Status = true;
}else if (localName.equals("Approved_Status"))
{
in_Approved_Status = true;
}
}
}