一、这里使用的是dom方式去解析的xml文件
这里直接贴代码
1.xml文件(文件名server.xml)内容【测试是放在assets文件夹下的】
<?xml version="1.0" encoding="UTF-8"?>
<result>
<code>200</code>
<msg>登录成功</msg>
<objbean>
<count>56</count>
<listbean>
<person id="1">
<name>张韶涵</name>
<age>58</age>
<tall>158cm</tall>
<job>歌手</job>
</person>
<person id="2">
<name>李德义</name>
<age>58</age>
<tall>168cm</tall>
<job>相士</job>
</person>
<person id="3">
<name>花儿</name>
<age>58</age>
<tall>88cm</tall>
<job>儿童</job>
</person>
<person id="4">
<name>张翰</name>
<age>58</age>
<tall>188cm</tall>
<job>演员</job>
</person>
<person id="5">
<name>月月</name>
<age>58</age>
<tall>158cm</tall>
<job>动漫人物</job>
</person>
<person id="6">
<name>龙哥</name>
<age>58</age>
<tall>158cm</tall>
<job>同事</job>
</person>
<person id="7">
<name>皓月</name>
<age>58</age>
<tall>48cm</tall>
<job>实物</job>
</person>
</listbean>
</objbean>
</result>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import center.demo.bean.Person;
import center.demo.utils.L;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private final String FILE_NAME = "server.xml";//assets中的文件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text);
try {
initData();
} catch (Exception e) {
e.printStackTrace();
L.d("报错了:错误开始\n" + e.getMessage() + "\n" + e.toString() + "\n错误结束");
}
}
private void initData() throws Exception {
ArrayList<Person> persons = new ArrayList<>();
int code = 0;
int count = 0;
String msg = "";
//获取assets文件的输入流
InputStream is = getResources().getAssets().open(FILE_NAME);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(is);
Element root = document.getDocumentElement();//返回文檔的根元素
NodeList rootNodes = root.getChildNodes();
//解析最外层
for (int r = 0; r < rootNodes.getLength(); r++) {
if (rootNodes.item(r).getNodeType() == Node.ELEMENT_NODE) {
if ("code".equals(rootNodes.item(r).getNodeName())) {
code = new Integer(rootNodes.item(r).getFirstChild().getNodeValue());
}
if ("msg".equals(rootNodes.item(r).getNodeName())) {
msg = rootNodes.item(r).getFirstChild().getNodeValue();
}
//获取第二层元素
if ("objbean".equals(rootNodes.item(r).getNodeName())) {
Element objBean = (Element) rootNodes.item(r);
NodeList objBeanNodes = objBean.getChildNodes();//获取跟元素包含的实体列表
for (int o = 0; o < objBeanNodes.getLength(); o++) {
if ("count".equals(objBeanNodes.item(o).getNodeName())) {
count = new Integer(objBeanNodes.item(o).getFirstChild().getNodeValue());
}
//获取第三层元素
if ("listbean".equals(objBeanNodes.item(o).getNodeName())) {
Element listBean = (Element) objBeanNodes.item(o);
NodeList personNodes = listBean.getElementsByTagName("person");//获取跟元素包含的实体列表
for (int i = 0; i < personNodes.getLength(); i++) {
Element personElement = (Element) personNodes.item(i);
int id = new Integer(personElement.getAttribute("id"));//获取每一个person的id属性值
Person person = new Person();
person.setId(id);
NodeList childNodes = personElement.getChildNodes();
for (int y = 0; y < childNodes.getLength(); y++) {
if (childNodes.item(y).getNodeType() == Node.ELEMENT_NODE) {
if ("name".equals(childNodes.item(y).getNodeName())) {
String name = childNodes.item(y).getFirstChild().getNodeValue();
person.setName(name);
}
if ("age".equals(childNodes.item(y).getNodeName())) {
String age = childNodes.item(y).getFirstChild().getNodeValue();
person.setAge(new Integer(age));
}
if ("job".equals(childNodes.item(y).getNodeName())) {
String job = childNodes.item(y).getFirstChild().getNodeValue();
person.setJob(job);
}
if ("tall".equals(childNodes.item(y).getNodeName())) {
String tall = childNodes.item(y).getFirstChild().getNodeValue();
person.setTall(tall);
}
}
}
persons.add(person);
}
}
}
}
}
}
is.close();//关闭流
StringBuffer buffer = new StringBuffer();
buffer.append("code:" + code + "\nmsg:" + msg + "\ncount:" + count + "\n\n");
buffer.append(persons.toString());
textView.setText(buffer.toString());
L.d(buffer.toString());//打印日志
}
}
3.Person实体类
public class Person {
private int id;
private String name;
private int age;
private String tall;
private String job;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTall() {
return tall;
}
public void setTall(String tall) {
this.tall = tall;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
4.解析结果