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

解析XML与DOM解析实现数据转换
本文详细解释了XML文档的基本概念,通过DOM(文档对象模型)如何操作和解析XML文档,具体展示了如何将XML内容转换为实体类对象,并通过服务类进行解析和数据处理。同时介绍了MainActivity活动类中如何利用解析后的数据填充ListView组件。

  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));
    }
    

}

理解起来是有难度的。

转载于:https://www.cnblogs.com/will-peng/archive/2013/03/18/2967062.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值