Android XML——DOM解析

XML解析

    • DOM解析

DOM解析

常用方法:
Document :getElementsByTagName(结点名称):根据结点的名称获得结点
NodeList:getchildNode();获得当前节点所有的子节点
getAttributes();获得当前结点所有的属性和值
getNodeValue();获得当前结点的值
getTextContent();获得当前子节点的文本
getNodeName();获得当前节点的名称

Activity代码              
// An highlighted block
public class MainActivity extends AppCompatActivity {
    private ArrayList<BookBean> bookBeans;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dom();
        Log.e("########list",bookBeans.get(0).toString());
        Log.e("########list",bookBeans.get(1).toString());
    }

    public void dom(){
        bookBeans = new ArrayList<>();
        DocumentBuilderFactory domfactory = DocumentBuilderFactory.newInstance();//获得解析工厂
        try {
            DocumentBuilder builder = domfactory.newDocumentBuilder();//创建builder对象
            InputStream inputStream = getAssets().open("book.xml");//获得xml文件
            Document document = builder.parse(inputStream);//把文件读入到内存中

            NodeList list = document.getElementsByTagName("book");//获得文件中所有节点名为book的内容,并打算将这些内容存入到list里面
            //由于有好多个book结点,所有我们要读list左遍历
            for(int i=0;i<list.getLength();i++){
                 BookBean bookBean =  new BookBean();

                //这样就拿到了其中一个book结点的内容,也就是一个对象,包括5个属性
                Node node = list.item(i);//当i等于0的时候,获得的是第一个book对象,在文件中一共有2个book对象
                //对象的结点找到了就是node,直接从node中获得属性,


                //接下来获得属性
                String id = node.getAttributes().getNamedItem("id").getNodeValue();//这个得到id这个属性
                Log.e("#########id",id);
                bookBean.setId(id);

                NodeList nodeList = node.getChildNodes();
                int length = nodeList.getLength();
                Log.e("#########length",length+"");
                for(int j=0;j<length;j++){
                    Node item = nodeList.item(j);
                    if("name".equals(item.getNodeName())){
                        String name = item.getTextContent();
                        Log.e("#########name",name);
                        bookBean.setName(name);
                    }else if("author".equals(item.getNodeName())){
                        String author = item.getTextContent();
                        Log.e("#########author",author);
                        bookBean.setAuthor(author);
                    }else if("year".equals(item.getNodeName())){
                        String year = item.getTextContent();
                        Log.e("#########year",year);
                        bookBean.setYear(year);
                    }else if("price".equals(item.getNodeName())){
                        String price = item.getTextContent();
                        Log.e("#########price",price);
                        bookBean.setPrice(price);
                    }
                }

                bookBeans.add(bookBean);

            }

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

实体类代码

// An highlighted block
public class BookBean {
    String id;
    String name;
    String author;
    String year;
    String price;


    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "BookBean{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", year='" + year + '\'' +
                ", price='" + price + '\'' +
                '}';
    }

Android XML——DOM解析_第1张图片

你可能感兴趣的:(Android XML——DOM解析)