一丶XML的創(chuàng)建
公司專注于為企業(yè)提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、成都商城網(wǎng)站開(kāi)發(fā),小程序開(kāi)發(fā),軟件定制制作等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。憑借多年豐富的經(jīng)驗(yàn),我們會(huì)仔細(xì)了解各客戶的需求而做出多方面的分析、設(shè)計(jì)、整合,為客戶設(shè)計(jì)出具風(fēng)格及創(chuàng)意性的商業(yè)解決方案,創(chuàng)新互聯(lián)更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務(wù)。手動(dòng)創(chuàng)建(保存到sd卡)
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
// 獲取sd卡的路徑
String path = Environment.getExternalStorageDirectory()
+ "/sms.xml";
// 創(chuàng)建file對(duì)象
File file = new File(path);
// 創(chuàng)建輸出流對(duì)象
try {
FileOutputStream fos = new FileOutputStream(file);
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append("
for (int i = 0; i < list.size(); i++) {
Sms sms = list.get(i);
sb.append("
sb.append("" + sms.getAddress() + "");
sb.append("" + sms.getBody() + "");
sb.append("
sb.append("
}
sb.append("
fos.write(sb.toString().getBytes());
fos.close();
Toast.makeText(this, "創(chuàng)建xml文件成功", 0).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "創(chuàng)建xml文件失敗", 0).show();
}
}else{
Toast.makeText(this, "sd卡不可用", 0).show();
}
調(diào)用系統(tǒng)的api創(chuàng)建
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
// 獲取sd卡的路徑
String path = Environment.getExternalStorageDirectory()
+ "/sms.xml";
//創(chuàng)建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卡不可用,請(qǐng)檢查sd的狀態(tài)",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:
// 拿到標(biāo)簽的名字
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)) {
// 將對(duì)象放到集合中
list.add(weather);
}
break;
}
// 獲取下一個(gè)事件對(duì)象
type = pull.next();
}
} catch (Exception e) {
e.printStackTrace();
}
//打印出來(lái)
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("城市風(fēng)力:" + w.getWind());
System.out.println("城市氣溫:" + w.getTemp());
System.out.println("---------------------------------");
}
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開(kāi)啟,新人活動(dòng)云服務(wù)器買多久送多久。