1.生成xml文件的两种方式
(1)采用拼接的方式生成xml(不推荐使用)
(2)利用XmlSerializer类生成xml文件
package com.example.lucky.test52xml; import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import android.widget.Button; import org.xmlpull.v1.XmlSerializer; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity {
Button bt1_generateXml;
Button bt2_generateXml;
List<Sms> smsList; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1_generateXml=findViewById(R.id.bt1_generateXml);
bt2_generateXml=findViewById(R.id.bt2_generateXml);
smsList=new ArrayList<>();
for (int i = 0; i <10; i++) {
Sms sms=new Sms();
sms.setAddress("10088"+i);
sms.setBody("nihaolucky"+i);
sms.setDate("201"+i);
smsList.add(sms);
} //方法1采用拼接的方式生成xml(不推荐使用)
bt1_generateXml.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringBuffer sb=new StringBuffer();
//开始拼接XML文件头
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
//开始拼接xml根节点
sb.append("<smss>");
for (Sms sms : smsList) {
sb.append("<sms>"); //开始拼接address节点
sb.append("<address>");
sb.append(sms.getAddress());
sb.append("</address>"); //开始拼接body节点
sb.append("<body>");
sb.append(sms.getBody());
sb.append("</body>"); //开始拼接date节点
sb.append("<date>");
sb.append(sms.getDate());
sb.append("</date>"); sb.append("</sms>");
}
sb.append("</smss>"); //将数据保存到sd卡中
File file=new File(Environment.getExternalStorageDirectory().getPath(),"lucky.xml");
try {
FileOutputStream fos=new FileOutputStream(file); //文件输出流对应写操作
fos.write(sb.toString().getBytes());
fos.close(); //关闭流
} catch (Exception e) {
e.printStackTrace();
}
} }); bt2_generateXml.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//1.获取XmlSerializer类的实例,通过Xml这个工具列去获取
XmlSerializer serializer=Xml.newSerializer();
//2.设置XmlSerializer序列化器的参数
File file=new File(Environment.getExternalStorageDirectory().getPath(),"lucky2.xml");
FileOutputStream fos= null;
try {
fos = new FileOutputStream(file);
serializer.setOutput(fos,"utf-8");
} catch (Exception e) {
e.printStackTrace();
} //3.开始写xml文档开头
try {
serializer.startDocument("utf-8",true);
serializer.startTag(null,"smss");
//循环写sms节点
for (Sms sms : smsList) {
serializer.startTag(null,"sms"); //开始写address节点
serializer.startTag(null,"address");
serializer.text(sms.getAddress());
serializer.endTag(null,"address"); //开始写body节点
serializer.startTag(null,"body");
serializer.text(sms.getBody());
serializer.endTag(null,"body"); //开始写date节点
serializer.startTag(null,"date");
serializer.text(sms.getDate());
serializer.endTag(null,"date"); serializer.endTag(null,"sms");
}
serializer.endTag(null,"smss");
//写文档结尾
serializer.endDocument();
fos.close();//关闭流
} catch (Exception e) {
e.printStackTrace();
} }
});
}
}
2.xml文件的解析
(1)xml的数据来源于服务器,服务器的开发人员通过通过服务器的技术把数据包装成一个xml文件返回给客户端。
(2)Android 开发人员需要进行xml的解析,把我们关心的数据取出即可。
(3)服务器是以流的形式把数据返回。
源码部分:
<1>MainActivity.class
package com.example.lucky.test52xml; import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; import org.xmlpull.v1.XmlSerializer; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity {
Button bt1_generateXml;
Button bt2_generateXml;
TextView tv_show;
List<Sms> smsList; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1_generateXml=findViewById(R.id.bt1_generateXml);
bt2_generateXml=findViewById(R.id.bt2_generateXml);
tv_show=findViewById(R.id.tv_show); //获取资产管理者,来操作assets目录下的文件
try {
InputStream inputStream=getAssets().open("weather.xml");
List<Channel> weatherlist=WeatherParser.parserXml(inputStream); //调用我们定义的方法解析xml文件
StringBuffer sb1=new StringBuffer();
for (Channel channel : weatherlist) {
sb1.append(channel.toString()); //注意:channel的toString方法已经在Channel类中重写
}
tv_show.setText(sb1.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
<2>Channel类
package com.example.lucky.test52xml; public class Channel {
private String id;
private String city;
private String temp;
private String wind;
private String pm250; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getCity() {
return city;
} public void setCity(String city) {
this.city = city;
} public String getTemp() {
return temp;
} public void setTemp(String temp) {
this.temp = temp;
} public String getWind() {
return wind;
} public void setWind(String wind) {
this.wind = wind;
} public String getPm250() {
return pm250;
} public void setPm250(String pm250) {
this.pm250 = pm250;
} @Override
public String toString() {
return "Channel{" +
"id='" + id + '\'' +
", city='" + city + '\'' +
", temp='" + temp + '\'' +
", wind='" + wind + '\'' +
", pm250='" + pm250 + '\'' +
'}';
}
}
<3>WeatherParser.class
package com.example.lucky.test52xml; import android.util.Xml; import org.xmlpull.v1.XmlPullParser; import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; public class WeatherParser { //服务器是以流的形式把数据返回的
public static List<Channel> parserXml(InputStream inputStream)throws Exception{
List<Channel> weatherLists=null;
Channel channel=null; //1.获取XmlPullParser解析的实例
XmlPullParser pullParser= Xml.newPullParser();
//2.设置XmlPullParser的参数
pullParser.setInput(inputStream,"utf-8");
//3.获取事件类型
int type=pullParser.getEventType();
while (type!=XmlPullParser.END_DOCUMENT){
switch (type){
case XmlPullParser.START_TAG: //若pullParser所指的标签是开始标签(例如:<channel id ='1'>为开始标签,</channel>为结束标签)
//4.判断pullParser所指的标签名
if("weather".equals(pullParser.getName())){
//5.创建集合对象
weatherLists=new ArrayList<>();
}else if("channel".equals(pullParser.getName())){
//6.创建channel对象
channel=new Channel();
//7.获取id值
String id=pullParser.getAttributeValue(0);
channel.setId(id);
}else if("city".equals(pullParser.getName())){
//8.获取city的数据
String city=pullParser.nextText(); //获取<city>北京</city> 标签包裹的内容:北京
channel.setCity(city);
}else if("wind".equals(pullParser.getName())){
//8.获取wind的数据
String wind=pullParser.nextText();
channel.setWind(wind);
}else if("pm250".equals(pullParser.getName())){
//8.获取pm250的数据
String pm250=pullParser.nextText();
channel.setWind(pm250);
}
break; //跳出switch语句,执行type=pullParser.next();语句,让pullParser向下移动
case XmlPullParser.END_TAG: //解析结束标志
if("channel".equals(pullParser.getName())){
//Channel对象的数据存到集合中
weatherLists.add(channel);
}
break; }
//pullParser不停的向下解析
type=pullParser.next(); }
return weatherLists;
}
}