Android DOM 解析 XML样式一

时间:2021-09-23 20:32:54


Android DOM 解析 XML样式一


res/raw/person.xml

<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="001">
<name>张三</name>
<age>22</age>
</person>
<person id="002">
<name>李四</name>
<age>23</age>
</person>
<person id="003">
<name>王五</name>
<age>24</age>
</person>
</persons>



DomParse.java 
public class DomParse {
public static List<Person> parse(InputStream inputStream){
List<Person> persons=new ArrayList<Person>();
try {
//创建解析器
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document dom = builder.parse(inputStream);
//得到根节点,
Element root = dom.getDocumentElement();
//查找所有persons节点,
NodeList pNodes = root.getElementsByTagName("person");
//**********************遍历所有person节点**********************
for (int i = 0; i < pNodes.getLength(); i++) {
Person person = new Person();
//得到第一个person节点
Element pNode = (Element) pNodes.item(i);
//获取person节点的id属性值
person.setId(new Integer(pNode.getAttribute("id")));
//获取person节点下的所有子节点
NodeList cNodes = pNode.getChildNodes();
//**********************遍历person节点下的所有子节点**********************
for (int j = 0; j < cNodes.getLength(); j++) {
Node node = (Node) cNodes.item(j);
//判断是否为元素类型
if(node.getNodeType() == Node.ELEMENT_NODE){
Element cNode = (Element) node;
//判断是否为name和age元素
if ("name".equals(cNode.getNodeName())) {
person.setName(cNode.getFirstChild().getNodeValue());
}else if ("age".equals(cNode.getNodeName())) {
person.setAge(newShort(cNode.getFirstChild().getNodeValue()));
}
}
}
persons.add(person);
}
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return persons;
}
}


在主类中调用:

查看源码打印?
01 public class TestActivity extends Activity {
02     Button btn;
03     ListView listView;
04     List<String> list=new ArrayList<String>();
05     public void onCreate(Bundle savedInstanceState) {
06         super.onCreate(savedInstanceState);
07         setContentView(R.layout.main);
08         btn=(Button) findViewById(R.id.button);
09         listView=(ListView) findViewById(R.id.listView);
10         btn.setOnClickListener(new OnClickListener(){
11             public void onClick(View arg0) {
12                 InputStream inputStream=getResources().openRawResource(R.raw.person);
13                 List<Person>persons = DomParse.parse(inputStream);
14                 for (Iterator iterator = persons.iterator(); iterator.hasNext();) {
15                     Person person = (Person) iterator.next();
16                     list.add(String.valueOf("ID:"+person.getId()) + " 姓名: "
17                             + person.getName() + " 年龄: " + person.getAge());
18                 }
19                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(
20                         getApplicationContext(),
21                         android.R.layout.simple_list_item_1,
22                         list);
23                 listView.setAdapter(adapter);
24             }
25         });
26     }
27 }