您好,登錄后才能下訂單哦!
一丶XML的創建
手動創建(保存到sd卡)
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
// 獲取sd卡的路徑
String path = Environment.getExternalStorageDirectory()
+ "/sms.xml";
// 創建file對象
File file = new File(path);
// 創建輸出流對象
try {
FileOutputStream fos = new FileOutputStream(file);
StringBuilder sb = new StringBuilder();
sb.append("<?xml version='1.0' encoding='UTF-8' ?>");
sb.append("<Smses>");
for (int i = 0; i < list.size(); i++) {
Sms sms = list.get(i);
sb.append("<sms>");
sb.append("<address>" + sms.getAddress() + "</address>");
sb.append("<body>" + sms.getBody() + "</body>");
sb.append("<date>" + sms.getDate() + "</date>");
sb.append("</sms>");
}
sb.append("</Smses>");
fos.write(sb.toString().getBytes());
fos.close();
Toast.makeText(this, "創建xml文件成功", 0).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "創建xml文件失敗", 0).show();
}
}else{
Toast.makeText(this, "sd卡不可用", 0).show();
}
調用系統的api創建
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
// 獲取sd卡的路徑
String path = Environment.getExternalStorageDirectory()
+ "/sms.xml";
//創建xml文件的生成器
try {
XmlSerializer serialiaer = Xml.newSerializer() ;
FileOutputStream os = new FileOutputStream(path) ;
serialiaer.setOutput(os, "UTF-8") ;
serialiaer.startDocument("UTF-8", true) ;
serialiaer.startTag(null, "Smses") ;
for (int i = 0; i < list.size(); i++) {
serialiaer.startTag(null, "sms") ;
serialiaer.startTag(null, "address") ;
serialiaer.text(list.get(i).getAddress()) ;
serialiaer.endTag(null, "address") ;
serialiaer.startTag(null, "body") ;
serialiaer.text(list.get(i).getBody()) ;
serialiaer.endTag(null, "body") ;
serialiaer.startTag(null, "date") ;
serialiaer.text(list.get(i).getDate()) ;
serialiaer.endTag(null, "date") ;
serialiaer.endTag(null, "sms") ;
}
serialiaer.endTag(null, "Smses") ;
serialiaer.endDocument() ;
Toast.makeText(this,"生成xml文件成功",0).show() ;
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this,"生成xml文件失敗",0).show() ;
}
}else{
Toast.makeText(this,"sd卡不可用,請檢查sd的狀態",0).show() ;
}
二丶XML的解析
// 加載文件
try {
// FileInputStream fis = new FileInputStream(
// "/data/data/com.xunfang.pull/weather.xml");
InputStream fis = MainActivity.class.getClassLoader().getResourceAsStream("weather.xml") ;
// 生成xml的pull解析器
XmlPullParser pull = Xml.newPullParser();
pull.setInput(fis, "UTF-8");
int type = pull.getEventType();
Weather weather = null;
while (type != XmlPullParser.END_DOCUMENT) {
// 判斷
switch (type) {
case XmlPullParser.START_TAG:
// 拿到標簽的名字
String tagName = pull.getName();
if ("channel".equals(tagName)) {
weather = new Weather();
weather.setId(pull.getAttributeValue(0));
}
if ("city".equals(tagName)) {
weather.setName(pull.nextText());
}
if ("temp".equals(tagName)) {
weather.setTemp(pull.nextText());
}
if ("wind".equals(tagName)) {
weather.setWind(pull.nextText());
}
break;
case XmlPullParser.END_TAG:
String name = pull.getName();
if ("channel".equals(name)) {
// 將對象放到集合中
list.add(weather);
}
break;
}
// 獲取下一個事件對象
type = pull.next();
}
} catch (Exception e) {
e.printStackTrace();
}
//打印出來
for (int i = 0; i < list.size(); i++) {
Weather w = list.get(i);
System.out.println("城市名稱:" + w.getName());
System.out.println("城市id:" + w.getId());
System.out.println("城市風力:" + w.getWind());
System.out.println("城市氣溫:" + w.getTemp());
System.out.println("---------------------------------");
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。