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>  


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.NamedNodeMap;   
  12. import org.w3c.dom.Node;   
  13. import org.xml.sax.SAXException;   
  14.   
  15. /**  
  16.  * 循环遍历获取所有节点  
  17.  * @author Administrator  
  18.  *  
  19.  */  
  20. public class DOMPrinter {   
  21.   
  22.     /**  
  23.      * 打印节点名称  
  24.      * @param node  
  25.      */  
  26.     public static void printNodeInfo(Node node)   
  27.     {   
  28.         System.out.println(node.getNodeName()+" : "+node.getNodeValue());   
  29.     }   
  30.        
  31.     /**  
  32.      * 通过节点判断  
  33.      * @param node  
  34.      */  
  35.     public static void printNode(Node node)   
  36.     {   
  37.         short nodeType=node.getNodeType();   
  38.         switch(nodeType)   
  39.         {   
  40.         //指令节点   
  41.         case Node.PROCESSING_INSTRUCTION_NODE:   
  42.             System.out.println("-----------PI start-----------");   
  43.             printNodeInfo(node);   
  44.             System.out.println("-----------PI end-----------");   
  45.             break;   
  46.         //元素节点   
  47.         case Node.ELEMENT_NODE:   
  48.             System.out.println("-----------Element start-----------");   
  49.             printNodeInfo(node);   
  50.             System.out.println("-----------Element end-----------");   
  51.                
  52.             NamedNodeMap attrs=node.getAttributes();   
  53.             int attrNum=attrs.getLength();   
  54.             for(int i=0;i<attrNum;i++)   
  55.             {   
  56.                 Node attr=attrs.item(i);   
  57.                 System.out.println("-----------Attribute start-----------");   
  58.                 printNodeInfo(attr);   
  59.                 System.out.println("-----------Attribute end-----------");   
  60.             }   
  61.             break;   
  62.         case Node.TEXT_NODE:   
  63.             System.out.println("-----------Text start-----------");   
  64.             printNodeInfo(node);   
  65.             System.out.println("-----------Text end-----------");   
  66.             break;   
  67.         default:   
  68.             break;   
  69.         }   
  70.            
  71.         Node child=node.getFirstChild();   
  72.         while(child!=null)   
  73.         {   
  74.             printNode(child);   
  75.             child=child.getNextSibling();   
  76.         }   
  77.     }   
  78.        
posted @ 2009-01-08 16:20  猪鼻驴耳  阅读(101)  评论(0)    收藏  举报