当前位置: 移动技术网 > IT编程>移动开发>Android > Android应用中使用DOM方式解析XML格式数据的基本方法

Android应用中使用DOM方式解析XML格式数据的基本方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

罗湖区教育信息网,宁国论坛,穿越之毁人不倦

dom比sax更容易掌握,因为她没有涉及回调和复杂的状态管理,然而,dom的实现常常将所有的xml节点保存在内存中,这样使的处理较大的文档效率低。
xml基本的节点类型
node - dom基本的数据类型
element - 最主要处理的对象是element
attr - 元素的属性
text - 一个element 或者attr的实际内容
document - 代表整个xml文档,一个document对象通常也称为一颗dom树
 
1.在src目录下新建一个android.xml

<?xml version="1.0" encoding="utf-8"?> 
<persons> 
  <person id="23"> 
    <name>xiaanming</name> 
    <age>23</age> 
  </person> 
  <person id="20"> 
    <name>liudehua</name> 
    <age>28</age> 
  </person> 
</persons> 

2.新建一个person对象来存放解析的内容

package com.example.dom_parser; 
 
public class person { 
  private int id; 
  private string name; 
  private int age; 
   
  public person(){} 
   
  public person(int id, string name, int age){ 
    this.id = id; 
    this.name = name; 
    this.age = age; 
  } 
 
  public int getid() { 
    return id; 
  } 
 
  public void setid(int id) { 
    this.id = id; 
  } 
 
  public string getname() { 
    return name; 
  } 
 
  public void setname(string name) { 
    this.name = name; 
  } 
 
  public int getage() { 
    return age; 
  } 
 
  public void setage(int age) { 
    this.age = age; 
  } 
   
  @override 
  public string tostring() { 
    return "id = " + id + ", name = " + name + ", age = " + age; 
  } 
   
} 

3 新建一个dompersonservice.class,注释我写的清楚,大家自己看

package com.example.dom_parser; 
 
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.util.log; 
 
 
public class dompersonservice { 
   
   
  public static list<person> readxml() throws throwable{ 
    //获得android.xml文件的输入流 
    inputstream is = mainactivity.class.getclassloader().getresourceasstream("android.xml"); 
    list<person> persons = new arraylist<person>(); 
     
    //实例化documentbuilderfactory和documentbuilder,并创建document 
    documentbuilderfactory factory = documentbuilderfactory.newinstance(); 
    documentbuilder builder = factory.newdocumentbuilder(); 
    document document = builder.parse(is); 
     
    //返回文档的根(root)元素 
    element rootelement = document.getdocumentelement(); 
     
    //获取一个note(dom基本的数据类型)集合,这里有两个person note 
    nodelist nodes = rootelement.getelementsbytagname("person"); 
     
    //遍历note集合 
    for(int i=0; i<nodes.getlength(); i++){ 
      //先从第一个person元素开始解析 
      element personelement = (element) nodes.item(i); 
      person person = new person(); 
      person.setid(integer.valueof(personelement.getattribute("id"))); 
       
      //获取person下面的name 和 age 的note集合 
      nodelist chilenodes = personelement.getchildnodes(); 
      for(int y=0; y<chilenodes.getlength(); y++){ 
        node childnode = chilenodes.item(y); 
         
        //判断子note的类型为元素note 
        if(childnode.getnodetype() == node.element_node){ 
          element childelement = (element) childnode; 
        if("name".equals(childelement.getnodename())){ 
          person.setname(childelement.getfirstchild().getnodevalue()); 
        }else if("age".equals(childelement.getnodename())){ 
          person.setage(integer.valueof(childelement.getfirstchild().getnodevalue())); 
        } 
        } 
      } 
       
      log.e("log", person.tostring()); 
       
      persons.add(person); 
    } 
     
    return persons; 
     
  } 
} 

关于dom解析xml,我们要清楚的知道个节点之间的关系,才能更好的操作对象树,值得注意的是在建立element时,要注意jar包的导入, 要选择org.w3c.dom.element,而不是其他的包。

ps:dom解析虽然我们在android中并不推荐使用,但是这并不代表着不可以实现。dom的原理是把xml文件的各种部分都看成是节点,所有的节点因为层级关系最后形成了一颗节点树。而dom的解析方式便是在内存中生存这棵树,并允许用户进行相关的操作。

这里列出几个dom中经常用到的方法:

node 接口的常用方法

一个节点可以调用
short getnodetype()
方法返回一个表示节点类型的常量(node接口规定的常量值),例如,对于element节点,getnodetype()方法返回的值为:
node.element_node
节点可以调用
nodelist  getchildnodes()
返回一个由当前节点的所有子节点组成的nodelist对象。节点调用
node  getfirstchild()
返回当前节点的第一个子节点。节点调用
node  getlastchild()
返回当前节点的最后一个子节点。节点可以调用
nodelist  gettextcontent()
返回当前节点及所有子孙节点中的文本内容。

还有其他的很多很多方法,我们可以通过api来详细了解。因为这里主要是为了学习android,所以关于dom稍微了解下就可以了。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网