Android XML数据的三种解析方式(Dom解析)

XML代表可扩展标记语言。XML是一种非常流行的格式,通常用于在Internet上共享数据。本章介绍如何解析XML文件并从中提取必要的信息。

android提供三种类型的XML解析器,分别是dom、sax和xmlpullparser。其中android推荐使用xmlpullparser,因为它既高效又易于使用。所以我们将使用xmlpullparser来解析XML。

首先先在Assets目录下创建file.xml 加入如下的内容

<?xml version="1.0"?>
<records>
    <employee>
        <name>张飞</name>
        <surname>张</surname>
        <salary>50000</salary>
    </employee>

    <employee>
        <name>孙权 </name>
        <surname>孙</surname>
        <salary>60000</salary>
    </employee>

    <employee>
        <name>鲁智深</name>
        <surname>鲁</surname>
        <salary>70000</salary>
    </employee>

</records>

接着修改activity_main.xml 布局文件的内容

<?xml version="1.0" encoding="utf-8"?>
<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">

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</RelativeLayout>

编写activity代码

package com.example.abc.xmlpaserdemo;

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 javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class MainActivity extends AppCompatActivity {
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.textView1);

        try {
            InputStream is = getAssets().open("file.xml");

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(is);

            Element element=doc.getDocumentElement();
            element.normalize();

            NodeList nList = doc.getElementsByTagName("employee");

            for (int i=0; i<nList.getLength(); i++) {

                Node node = nList.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element2 = (Element) node;
                    tv.setText(tv.getText()+"\nName : " + getValue("name", element2)+"\n");
                    tv.setText(tv.getText()+"Surname : " + getValue("surname", element2)+"\n");
                    tv.setText(tv.getText()+"-----------------------");
                }
            }

        } catch (Exception e) {e.printStackTrace();}

    }

    private static String getValue(String tag, Element element) {
        NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
        Node node = nodeList.item(0);
        return node.getNodeValue();
    }
}

效果图

原创文章 29 获赞 1 访问量 9273

猜你喜欢

转载自blog.csdn.net/qinxuexiang_blog/article/details/91467857