編輯:關於android開發
1.首先在SD卡上新建一個測試文件夾,用來保存生成的XML
// 獲取保存路徑
public File getFilePath() {
File filePath = null;
//判斷SD卡存在與否
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
filePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+
"/A_Test/test/");
if (!filePath.isDirectory()) {//判斷文件存在與否,不存在就創建
filePath.mkdirs();
}
} else {
Toast.makeText(Main.this, "存儲卡不存在,請插入卡!", 3000).show();
}
return filePath;
}
注意:權限的添加:
<!-- SD卡寫入 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DELETE_CACHE_FILES" />
2.生成一個測試的XML文件
File file = new File(getFilePath() + "/test.xml");
if (!file.exists()) {// 如果文件不存在,就創建新文件
System.out.println("文件不存在,馬上創建!");
List<PersonModel> personModel = new ArrayList<PersonModel>();
personModel.add(new PersonModel(1001, "張三", (short) 30));
personModel.add(new PersonModel(1002, "李四", (short) 18));
personModel.add(new PersonModel(1003, "王二", (short) 21));
CreateXML.createXML(file, personModel);
}
/**
* 生成XML文件
*
* @author lilin
* @date 2011-9-26 上午08:36:08
* @ClassName: CreateXML
* @Description: TODO
*/
public class CreateXML {
// 生成XML文件
public static void createXML(File file, List<PersonModel> personModel)
throws IllegalArgumentException, IllegalStateException, IOException {
FileOutputStream outputStream = null;
OutputStreamWriter writer = null;
outputStream = new FileOutputStream(file);
writer = new OutputStreamWriter(outputStream, "UTF-8");
BufferedWriter writer2 = new BufferedWriter(writer);// 增加的緩存功能
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(writer2);
serializer.setOutput(outputStream, "utf-8");
serializer.startDocument("utf-8", true);
serializer.startTag(null, "persons");
for (PersonModel persons : personModel) {
serializer.startTag(null, "person");
serializer.attribute(null, "id", persons.getId().toString());
serializer.startTag(null, "name");
serializer.text(persons.getName());
serializer.endTag(null, "name");
serializer.startTag(null, "age");
serializer.text(persons.getAge().toString());
serializer.endTag(null, "age");
serializer.endTag(null, "person");
}
serializer.endTag(null, "persons");
serializer.endDocument();
outputStream.close();
}
}
3.解析XML
FileInputStream fileInputStream = new FileInputStream(file);
List<PersonModel> personModel = PullXML.pullXML(fileInputStream);
for (int i = 0; i < personModel.size(); i++) {
System.out.println(personModel.get(i).getId() + "--"
+ personModel.get(i).getName() + "--"
+ personModel.get(i).getAge());
}
/**
* 解析XML
*
* @author lilin
* @date 2011-9-26 上午09:23:37
* @ClassName: PullXML
* @Description: TODO
*/
public class PullXML {
public static List<PersonModel> pullXML(InputStream inStream)
throws Throwable {
List<PersonModel> persons = null;
PersonModel person = null;
/* 聲明XML的pull解析器 */
XmlPullParser parser = Xml.newPullParser();
/* 聲明編碼方式 */
parser.setInput(inStream, "UTF-8");
int eventType = parser.getEventType();// 產生第一個事件
while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文檔結束事件
switch (eventType) {
case XmlPullParser.START_DOCUMENT:// 0 文檔開始事件
persons = new ArrayList<PersonModel>();
break;
case XmlPullParser.START_TAG:// 2 開始元素
String name = parser.getName();// 獲取解析器當前指向的元素的名稱
if ("person".equals(name)) {
person = new PersonModel();
person.setId(new Integer(parser.getAttributeValue(0)));
}
if (person != null) {
if ("name".equals(name)) {
person.setName(parser.nextText());// 獲取解析器當前指向元素的下一個文本節點的值
}
if ("age".equals(name)) {
person.setAge(new Short(parser.nextText()));
}
}
break;
case XmlPullParser.END_TAG:
if ("person".equals(parser.getName())) {
persons.add(person);
person = null;
}
break;
}
eventType = parser.next();
}
return persons;
}
}
Android App 開發技能圖譜,androidapp 操作系統 Windows/MacOSX/Linux 編程語言 Java HTML/JS (Hybrid/
【騰訊Bugly干貨分享】基於RxJava的一種MVP實現,buglyrxjava本文來自於騰訊bugly開發者社區,非經作者同意,請勿轉載,原文地址:http://de
手機安全衛士——緩存清理,安全衛士緩存清理CleanCacheActivity.java /** * 緩存清理*/ public class CleanCacheAc
在Windows下快速搭建React Native Android開發環境 搭建JAVA開發環境 根據操作系統分為x86或x64位的,下載jdk1.8以上的版本, 本