android dom解析相关理解

DOM解析XML
DOM是Document Object Model的缩写,即文档对象模型.DOM解析器是通过将XML文档解析成树状模型并将其放入内存来完成解析工作的,而后对文档的操作都是在这个树状模型上完成.这个在内存中的文档是实际文档大小的几倍.好处是结构清晰,操作方便,坏处是耗费系统资源.要使用DOM方式来解析xml,需引入下面两个包:
importjavax.xml.parsers.*;//包含DOM解析器和SAX解析器的具体实现
import org.w3c.dom.*;//定义了W3C制定的DOM接口
具体思路:
*首先创建一个DocumentBuilderFactory实例,
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

*利用DocumentBuilderFactory实例创建DocumentBuilder,
DocumentBuilder builder = factory.newDocumentBuilder();
*然后加载xml文档(Document),
Document doc = builder.parse(getAssets().open("xml/student.xml"));
*然后获取文档的根节点(Element),
Element root = doc.getDocumentElement();
*然后获取根节点中所有子节点的列表(NodeList),
NodeListnodeList = root.getElementsByTagName("student");
*然后再获取子节点列表中需获取的节点.
**************************************相关代码*****************
//student是一个实体类,里面有name,age参数,构造方法,和getter and setter方法,tostring输出;
public class DomParserActivity extends Activity {
private TextView mtextview;
private ListView mlistview;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.domparser);
mtextview = (TextView) findViewById(R.id.textView1);
mlistview = (ListView) findViewById(R.id.listView1);
mtextview.setText("dom解析xml");
List<Student> list = parserxml();
ArrayAdapter< Student> adapter = new ArrayAdapter<Student>(this,android.R.layout.simple_expandable_list_item_1,list);
mlistview.setAdapter(adapter);

}
private List<Student> parserxml() {
List<Student> list = new ArrayList<Student>();
try {
//获取工厂实例
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//获取解析器实例
DocumentBuilder db;

db = dbf.newDocumentBuilder();

//// 用解析器解析Xml, 放于src下面
Document doc = db.parse(DomParserActivity.this.getClassLoader().getResourceAsStream("student.xml"));
//获取Document根节点
Element ele = doc.getDocumentElement();
//拿到某个标签的节点列表
NodeList nodelist = ele.getElementsByTagName("student");
int length = nodelist.getLength();
Student stu = null;
//遍历节点列表
for(int i=0;i<length;i++){
stu = new Student();
Element stuele = (Element) nodelist.item(i);
    stu.setId(Integer.parseInt(stuele.getAttribute("id")));
  //拿到<student>标签的子节点列表
    NodeList childlist = stuele.getChildNodes();
    int childlength = childlist.getLength();
  //遍历子节点列表
    for(int j=0;j<childlength;j++){
    Node node = childlist.item(j);
   if(node.getNodeType()==Node.ELEMENT_NODE){
     Element element = (Element) node;
     if("name".equals(element.getNodeName())){
    stu.setName(element.getFirstChild().getNodeValue());
    Log.i("TAG", ""+stu.getName());
     }
     else if("age".equals(element.getNodeName())){
    stu.setAge(Integer.valueOf(element.getAttribute("value")));
   
     }
    
    }
   }
    list.add(stu);
}
}
catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return list;

}
 
}

你可能感兴趣的:(android,xml,dom,ArrayAdapter,document)