XML 文件的操作(一)

最近想学习一下XML读取的操作。我会把各种读取XML的方法写成程序。希望大家给点意见。XML文件
Xml代码 复制代码
  1. <?xml version="1.0" encoding="gb2312"?>  
  2.   
  3. <?xml-stylesheet type="text/xsl" href="students.xsl"?>  
  4.   
  5. <students>  
  6.     <student sn="01">  
  7.         <name>张三</name>  
  8.         <age>18</age>  
  9.     </student>  
  10.        
  11.     <student sn="02">  
  12.         <name>李四</name>  
  13.         <age>20</age>  
  14.     </student>  
  15. </students>  

读取XML文件,使用的DOM
Java代码 复制代码
  1. package com.ibm.xml;   
  2.   
  3. import java.io.File;   
  4. import java.io.IOException;   
  5.   
  6. import javax.xml.parsers.DocumentBuilder;   
  7. import javax.xml.parsers.DocumentBuilderFactory;   
  8. import javax.xml.parsers.ParserConfigurationException;   
  9.   
  10. import org.w3c.dom.Document;   
  11. import org.w3c.dom.Element;   
  12. import org.w3c.dom.Node;   
  13. import org.w3c.dom.NodeList;   
  14. import org.xml.sax.SAXException;   
  15.   
  16. /**  
  17.  * DOM解析XML  
  18.  * 通过获取子节点方法  
  19.  * @author Administrator  
  20.  *  
  21.  */  
  22. public class DOMStudentsInfo {   
  23.   
  24.     /**  
  25.      * @param args  
  26.      */  
  27.     public static void main(String[] args) {   
  28.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();   
  29.         System.out.println(dbf.getClass().getName());   
  30.            
  31.         try {   
  32.             DocumentBuilder db=dbf.newDocumentBuilder();   
  33.             //读取文件   
  34.             Document doc=db.parse(new File("students.xml"));   
  35.             //获取子节点   
  36.             NodeList nl=doc.getElementsByTagName("student");   
  37.             int len=nl.getLength();   
  38.             //获取节点数   
  39.             System.out.println(len);   
  40.             for(int i=0;i<len;i++)   
  41.             {   
  42.                 Element eltStu=(Element)nl.item(i);   
  43.                    
  44.                 Node eltName=eltStu.getElementsByTagName("name").item(0);   
  45.                 Node eltAge=eltStu.getElementsByTagName("age").item(0);   
  46.                    
  47.                 String name=eltName.getFirstChild().getNodeValue();   
  48.                 String age=eltAge.getFirstChild().getNodeValue();   
  49.                    
  50.                 System.out.println("name: "+name);   
  51.                 System.out.println("age: "+age);   
  52.                 System.out.println("-------------------------");   
  53.             }   
  54.         } catch (ParserConfigurationException e) {   
  55.             e.printStackTrace();   
  56.         }catch (SAXException e) {   
  57.             // TODO Auto-generated catch block   
  58.             e.printStackTrace();   
  59.         } catch (IOException e) {   
  60.             // TODO Auto-generated catch block   
  61.             e.printStackTrace();   
  62.         }   
  63.     }   
  64.   
  65. }  
posted @ 2009-01-08 16:12  猪鼻驴耳  阅读(127)  评论(0)    收藏  举报