标签:des android style blog color io os 使用 java
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");
*然后再获取子节点列表中需获取的节点.
**************************************相关代码*****************
1 //student是一个实体类,里面有name,age参数,构造方法,和getter and setter方法,tostring输出; 2 public class DomParserActivity extends Activity { 3 private TextView mtextview; 4 private ListView mlistview; 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.domparser); 10 mtextview = (TextView) findViewById(R.id.textView1); 11 mlistview = (ListView) findViewById(R.id.listView1); 12 mtextview.setText("dom解析xml"); 13 List<Student> list = parserxml(); 14 ArrayAdapter< Student> adapter = new ArrayAdapter<Student>(this,android.R.layout.simple_expandable_list_item_1,list); 15 mlistview.setAdapter(adapter); 16 17 } 18 private List<Student> parserxml() { 19 List<Student> list = new ArrayList<Student>(); 20 try { 21 //获取工厂实例 22 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 23 //获取解析器实例 24 DocumentBuilder db; 25 26 db = dbf.newDocumentBuilder(); 27 28 //// 用解析器解析Xml, 放于src下面 29 Document doc = db.parse(DomParserActivity.this.getClassLoader().getResourceAsStream("student.xml")); 30 //获取Document根节点 31 Element ele = doc.getDocumentElement(); 32 //拿到某个标签的节点列表 33 NodeList nodelist = ele.getElementsByTagName("student"); 34 int length = nodelist.getLength(); 35 Student stu = null; 36 //遍历节点列表 37 for(int i=0;i<length;i++){ 38 stu = new Student(); 39 Element stuele = (Element) nodelist.item(i); 40 stu.setId(Integer.parseInt(stuele.getAttribute("id"))); 41 //拿到<student>标签的子节点列表 42 NodeList childlist = stuele.getChildNodes(); 43 int childlength = childlist.getLength(); 44 //遍历子节点列表 45 for(int j=0;j<childlength;j++){ 46 Node node = childlist.item(j); 47 if(node.getNodeType()==Node.ELEMENT_NODE){ 48 Element element = (Element) node; 49 if("name".equals(element.getNodeName())){ 50 stu.setName(element.getFirstChild().getNodeValue()); 51 Log.i("TAG", ""+stu.getName()); 52 } 53 else if("age".equals(element.getNodeName())){ 54 stu.setAge(Integer.valueOf(element.getAttribute("value"))); 55 56 } 57 58 } 59 } 60 list.add(stu); 61 } 62 } 63 catch (ParserConfigurationException e) { 64 // TODO Auto-generated catch block 65 e.printStackTrace(); 66 } catch (SAXException e) { 67 // TODO Auto-generated catch block 68 e.printStackTrace(); 69 } catch (IOException e) { 70 // TODO Auto-generated catch block 71 e.printStackTrace(); 72 } 73 74 75 return list; 76 77 } 78 79 }
标签:des android style blog color io os 使用 java
原文地址:http://www.cnblogs.com/xuanyuanzhuo-blog/p/3983485.html