public class CreatexmlActivity extends Activity {
Button btn1;
// OnClickListener ocl1;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
List<Person> list1=new ArrayList<Person>();
Person p1=new Person("shouji",12);
p1.setId(0);
list1.add(p1);
Person p2=new Person("dianhua",22);
p2.setId(new Integer(1));
list1.add(p2);
try {
saveAllByXmlSerializer(list1,new FileOutputStream(new File("/sdcard/data","b.xml")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<Person> list2;
try {
list2 = findAll(new FileInputStream(new File("/sdcard/data","b.xml")));
for(int i=0;i<list2.size();i++)
Toast.makeText(getApplicationContext(),list2.get(i).getName(),Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
public List<Person> findAll(InputStream in) throws Exception {
}
public void saveAllByXmlSerializer(List<Person> users, OutputStream out) throws IOException {
}
public class Person {
}
}
创建的xml文件如下:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><persons><person id="0"><name>shouji</name><age>12</age></person><person id="1"><name>dianhua</name><age>22</age></person></persons>
大家会发现这个xml文件和我们平常使用的xml文件有所差别,请仔细看!
发现没有,<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>这个中间的是单引号,而我们平常使用的是双引号<?xml version="1.0" encoding="utf-8"?>,这个怎么办呢?下期解决。