我有一个问题,我们希望你能提供帮助,现在我可以将XML DOM中的数据读取到Spinner或ListView,但我想要
怎么做? 我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<employees>
<employee id ="1" title="Human Resources Department ">
<name>Name A</name>
<phone>0909000111</phone>
</employee>
<employee id ="1" title="Human Resources Department ">
<name>Name B</name>
<phone>0909000112</phone>
</employee>
<employee id ="2" title="Technical Department ">
<name>Name A - 2</name>
<phone>0909000222</phone>
</employee>
<employee id ="2" title="Human Resources Department ">
<name>Name B - 2</name>
<phone>0909000223</phone>
</employee>
<employee id ="3" title="Sales Office ">
<name>Name C - 1</name>
<phone>0909000333</phone>
</employee>
<employee id ="3" title="Sales Office ">
<name>Name C - 2</name>
<phone>0909000333</phone>
</employee>
</employees>
所有文件类:Gods,Department,Employee和MainActivity
班神
package com.example.bai29_tulam;
import android.app.Activity;
public class Gods extends Activity{
String id, name;
public Gods(){
super();
}
public Gods(String id,String name){
this.id=id;
this.name=name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return this.id+" - "+this.name;
}
}
班级部门
package com.example.bai29_tulam;
import java.util.ArrayList;
import android.app.Activity;
public class Department extends Gods{
public ArrayList<Employee>listEmploy=null;
public Department(String id,String name){
super(id,name);
this.listEmploy=new ArrayList<Employee>();
}
public Department(){
super();
this.listEmploy=new ArrayList<Employee>();
}
public boolean addEmploy(Employee employee){
employee.setDeparment(this);
return listEmploy.add(employee);
}
public ArrayList<Employee>getListEmployee(){
return this.listEmploy;
}
public int size(){
return listEmploy.size();
}
public Employee get(int i){
return listEmploy.get(i);
}
}
班级员工
package com.example.bai29_tulam;
import android.app.Activity;
public class Employee extends Gods{
private Department dp;
public Department getDepartment(){
return dp;
}
public void setDeparment(Department depart){
this.dp=depart;
}
public Employee(String id,String name, Department depart){
super(id,name);
this.dp=depart;
}
public Employee(String id,String name){
super(id,name);
}
public Employee(){
super();
}
}
和类MainActivity
package com.example.bai29_tulam;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button btn_DOM, btn_SAX;
Spinner spin;
ListView lisview;
EditText editData;
TextView textData;
ArrayList<Employee> arrList = new ArrayList<Employee>();
ArrayAdapter<Employee> adapter;
ArrayList<Department> arrSpin = new ArrayList<Department>();
ArrayAdapter<Department> adapSpin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// editData = (EditText) findViewById(R.id.editData);
spin = (Spinner) findViewById(R.id.spinner);
adapSpin = new ArrayAdapter<Department>(this,
android.R.layout.simple_spinner_item, arrSpin);
adapSpin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapSpin);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
loadListEmployeeByDepartment(arrSpin.get(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
lisview = (ListView) findViewById(R.id.listView);
adapter = new ArrayAdapter<Employee>(this,
android.R.layout.simple_list_item_1, arrList);
lisview.setAdapter(adapter);
btn_DOM = (Button) findViewById(R.id.btn_DOM);
btn_SAX = (Button) findViewById(R.id.btn_SAX);
btn_DOM.setOnClickListener(this);
btn_SAX.setOnClickListener(this);
}
protected void loadListEmployeeByDepartment(Department department) {
// TODO Auto-generated method stub
// arrList.clear();
arrList.addAll(department.getListEmployee());
adapSpin.notifyDataSetChanged();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_DOM:
loadDataDOM();
break;
case R.id.btn_SAX:
loadDataSAX();
break;
}
}
private void loadDataSAX() {
// TODO Auto-generated method stub
}
private void loadDataDOM() {
// TODO Auto-generated method stub
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
// Create Path File
String sdcard = Environment.getExternalStorageDirectory()
.getAbsolutePath();
String xmfile = sdcard + "/employee.xml";
FileInputStream fIn = new FileInputStream(xmfile);
//Creat Document from builder to parser FileInput
Document doc = builder.parse(fIn);
//browse element from doc
Element root = doc.getDocumentElement();
NodeList list = root.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node instanceof Element) {
Element employee = (Element) node;
String id = employee.getAttribute("id");
String title = employee.getAttribute("title");
String temp = id + title;
NodeList listChild = employee.getElementsByTagName("name");
String name = listChild.item(0).getTextContent();
listChild = employee.getElementsByTagName("phone");
String phone = listChild.item(0).getTextContent();
Department dp = new Department();
dp.setId(id);
dp.setName(title);
if (temp.trim().equalsIgnoreCase(dp.toString())) {
Employee nvcpb = new Employee();
nvcpb.setId(name);
nvcpb.setName(phone);
nvcpb.setDeparment(dp);
arrList.add(nvcpb);
} else {
}
arrSpin.add(dp);
// arrSpin.add(nameSpin);
}
}
adapSpin.notifyDataSetChanged();
// textData.setText(datashow);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}