Android学习第十一天----Xml解析之DOM

  DOM= Document Object Model,文档对象模型,DOM可以以一种独立于平台和语言的方式访问和修改一个文档的内容和结构。换句话说,这是表示和处理一个HTML或XML文档的常用方法。

节点

根据 DOM,HTML 文档中的每个成分都是一个节点。
DOM 是这样规定的:
整个文档是一个文档节点
每个 HTML 标签是一个元素节点
包含在 HTML 元素中的文本是文本节点
每一个 HTML 属性是一个属性节点
注释属于注释节点
 
xml添加一个listview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

 

创建一个实体类,例如Persons

package com.example.entity;

public class Persons
{
    private Integer id;
    private String name;
    
    public Persons()
    {
        super();
    }

    public Persons(int id, String name)
    {
        super();
        this.id = id;
        this.name = name;
    }

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }
 
}

创建一个服务类:

package com.example.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.example.entity.Persons;

public class PersonParser
{
    public static List<Persons> parser(InputStream is)
    {
        
        List<Persons> list = new ArrayList<Persons>();
        
        try
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            
                Document doc = db.parse(is);
                
                Element root = doc.getDocumentElement();
                
                NodeList nodeList = root.getElementsByTagName("person");
                
                for (int i = 0; i < nodeList.getLength(); i++)
                {
                    Persons p = new Persons();
                    
                    Element element = (Element) nodeList.item(i);
                    p.setId(Integer.parseInt(element.getAttribute("id")));
                    p.setName(element.getFirstChild().getNodeValue());
                    list.add(p);
                }
                
        }
             catch (SAXException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         catch (ParserConfigurationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
        
    }
}

 

 

mainActivity中

package com.example.dom;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.entity.Persons;
import com.example.service.PersonParser;

public class MainActivity extends Activity
{
    private List<String> list = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ListView mListView = (ListView)findViewById(R.id.listView1);
        InputStream is = getResources().openRawResource(R.raw.persons);
        
        List<Persons> data = PersonParser.parser(is);
        
        for (Persons person : data)
        {
            list.add(person.getId()+"--"+person.getName());
        }
        
        mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list));
    }
    

}

理解起来是有难度的。

posted @ 2013-03-18 23:40  小三小山  阅读(147)  评论(0编辑  收藏  举报