XML解析---DOM

前端之家收集整理的这篇文章主要介绍了XML解析---DOM前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

设置DOM的一般步骤

  1. 创建目标类;
  2. 创建ArrayList,用来存放解析结果;
  3. 通过DocumentBuilderFactory.newInstance() 获取factory;
  4. 通过factoty获得buider实例;
  5. 取得Dom builder.parse(InputStream);
  6. 寻找根节点 dom.getDocumentElement();
  7. 通过根节点获取所有的item; root.getElementByTagName();
  8. 遍历所有的item;
  9. 可以获得attribute;
  10. 这时可以获取没个item下的子节点;
  11. 遍历每个子节点;
  12. 判断类型
  13. 根据childNode.getNodeName() 将对应的值 childNode.getFirstChild().getNodeValue() 存入目标类的对象中
  14. 添加对象至Arraylist中;

package com.example.xmltest;

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

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private Button dom_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dom_button = (Button) findViewById(R.id.dom);

        dom_button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                try {
                    doDom(MainActivity.this.getResources().getAssets()
                            .open("book.xml"));
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

    }

    private void doDom(InputStream inStream) throws Exception {
        List<Book> books = new ArrayList<Book>();
        DocumentBuilderFactoryfactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document dom = builder.parse(inStream);
        Element root = dom.getDocumentElement();
        NodeList item = root.getElementsByTagName("book");

        for (int i = 0; i < item.getLength(); i++) {
            Book book = new Book();
            Element bookNode = (Element) item.item(i);
            NodeList childNodes = bookNode.getChildNodes();

            for (int j = 0; j < childNodes.getLength(); j++) {
                Node node = childNodes.item(j);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element childnode = (Element) node;
                    if ("id".equals(childnode.getNodeName())) {
                        book.setId(childnode.getFirstChild().getNodeValue());
                    } else if ("name".equals(childnode.getNodeName())) {
                        book.setName(childnode.getFirstChild().getNodeValue());
                    } else if ("price".equals(childnode.getNodeName())) {
                        book.setPrize(childnode.getFirstChild().getNodeValue());
                    }
                }
            }
            books.add(book);

        }

        for(Book book:books)
        {
            System.out.println(book.toString());
        }
    }

}

猜你在找的XML相关文章