XML 文件的操作(六)jdom对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>  

JDOM对文件操作
Java代码 复制代码
  1. package com.ibm.xml;   
  2.   
  3. import java.io.File;   
  4. import java.io.IOException;   
  5.   
  6. import org.jdom.*;   
  7. import org.jdom.input.SAXBuilder;   
  8. import org.jdom.output.Format;   
  9. import org.jdom.output.XMLOutputter;   
  10.   
  11.   
  12. /**  
  13.  * JDOM对文件做增加、修改、删除  
  14.  * @author Administrator  
  15.  *  
  16.  */  
  17. public class JDOMConvert   
  18. {   
  19.   
  20.        
  21.     public static void main(String[] args)   
  22.     {   
  23.         SAXBuilder saxBuilder=new SAXBuilder();   
  24.         try  
  25.         {   
  26.             Document doc=saxBuilder.build(new File("students.xml"));   
  27.                
  28.             Element eltStu=new Element("student");   
  29.             Element eltName=new Element("name");   
  30.             Element eltAge=new Element("age");   
  31.                
  32.             eltName.setText("王五");   
  33.             eltAge.setText("19");   
  34.                
  35.             eltStu.addContent(eltName);   
  36.             eltStu.addContent(eltAge);   
  37.             eltStu.setAttribute("sn","03");   
  38.                
  39.             Element root=doc.getRootElement();   
  40.             root.addContent(eltStu);   
  41.                
  42.             root.removeChild("student");   
  43.                
  44.             root.getChild("student").getChild("age").setText("22");   
  45.                
  46.             XMLOutputter xmlOut=new XMLOutputter();   
  47.                
  48.             Format fmt=Format.getPrettyFormat();   
  49.             fmt.setIndent("    ");   
  50.             fmt.setEncoding("gb2312");   
  51.                
  52.             xmlOut.setFormat(fmt);   
  53.             try  
  54.             {   
  55.                 xmlOut.output(doc,System.out);   
  56.             }   
  57.             catch (IOException e)   
  58.             {   
  59.                 // TODO 自动生成 catch 块   
  60.                 e.printStackTrace();   
  61.             }   
  62.                
  63.         }   
  64.         catch (JDOMException e)   
  65.         {   
  66.             // TODO 自动生成 catch 块   
  67.             e.printStackTrace();   
  68.         }   
  69.         catch (IOException e)   
  70.         {   
  71.             // TODO 自动生成 catch 块   
  72.             e.printStackTrace();   
  73.         }   
  74.   
  75.     }   
  76.   
  77. }  
posted @ 2009-01-08 21:01  猪鼻驴耳  阅读(433)  评论(0)    收藏  举报