本文实例讲述了Android中DOM解析xml文件的方法。分享给大家供大家参考,具体如下:
一、在assets文件中写xml文件
<&#63;xml version="1.0" encoding="UTF-8"&#63;>李明 30 李向梅 25
二、在service中写一个DOM解析的操作
package com.example.service; 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 com.example.domain.Person; public class XMLDomService { public ListparseXML(InputStream is) { List list = new ArrayList (); // 创建DOM工厂对象 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { // DocumentBuilder对象 DocumentBuilder builder = factory.newDocumentBuilder(); // 获取文档对象 Document document = builder.parse(is); // 获取文档对象的root Element root = document.getDocumentElement(); // 获取persons根节点中所有的person节点对象 NodeList persOnNodes= root.getElementsByTagName("person"); // 遍历所有的person节点 for (int i = 0; i
三、在Activity中显示操作
package com.example.lession03_xml; import java.io.InputStream; import java.util.List; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import com.example.domain.Person; import com.example.service.XMLContentHandler; import com.example.service.XMLDomService; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.InputBinding; import android.widget.Button; import android.widget.Toast; public class XmlActivityextends Activity { //声明组件 public Button btn_sax,btn_dom,btn_pull; public XMLDomService xmlDomService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置显示的视图 setContentView(R.layout.activity_xml); xmlDomService=new XMLDomService(); //根据id获取组件 btn_sax=(Button) findViewById(R.id.btn_sax); btn_dom=(Button) findViewById(R.id.btn_dom); btn_pull=(Button) findViewById(R.id.btn_pull); //为按钮注册事件 btn_sax.setOnClickListener(new MyOnclickListener()); btn_dom.setOnClickListener(new MyOnclickListener()); btn_pull.setOnClickListener(new MyOnclickListener()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.xml, menu); return true; } //匿名类 class MyOnclickListener implements OnClickListener{ @Override public void onClick(View v) { int id=v.getId(); switch (id) { case R.id.btn_sax: Toast.makeText(XmlActivity.this,"采用SAX解析", Toast.LENGTH_LONG).show(); try{ //SAX解析的工厂对象 SAXParserFactory factory=SAXParserFactory.newInstance(); //得到sax的解析器 SAXParser saxParser=factory.newSAXParser(); //创建handler对象 XMLContentHandler handlerService=new XMLContentHandler(); InputStream is=getAssets().open("csdn.xml"); //直接解析 saxParser.parse(is, handlerService); //通过handlerService对象获取 Toast.makeText(XmlActivity.this,"----"+handlerService, Toast.LENGTH_LONG).show(); }catch(Exception e){ e.printStackTrace(); } break; case R.id.btn_dom: InputStream is=null; try{ //获取读取文件的输入流对象 is=getAssets().open("csdn.xml"); //采用dom解析 ListpersOns=xmlDomService.parseXML(is); //简单测试 //Toast.makeText(XmlActivity.this, ""+persons.get(0).getName(), Toast.LENGTH_LONG).show(); Toast.makeText(XmlActivity.this,"采用DOM解析"+persons.get(0).getName(), Toast.LENGTH_LONG).show(); }catch(Exception e){ e.printStackTrace(); } break; case R.id.btn_pull: Toast.makeText(XmlActivity.this,"采用PULL解析", Toast.LENGTH_LONG).show(); break; default: break; } } } }
希望本文所述对大家Android程序设计有所帮助。