使用JDOM生成和解析XML

到官方网站下载JDOM包http://www.jdom.org/ 

注意的是,版本1和版本2的类路径已经变更,如果你是更新使用版本2,则需要重新编译你的代码

 

Java代码   收藏代码
  1. package com.test;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.util.List;  
  5.   
  6. import org.jdom2.Document;  
  7. import org.jdom2.Element;  
  8. import org.jdom2.input.SAXBuilder;  
  9. import org.jdom2.output.Format;  
  10. import org.jdom2.output.XMLOutputter;  
  11.   
  12. /** 
  13.  * @说明 JDom生成解析XML 
  14.  * @author cuisuqiang 
  15.  * @version 1.0 
  16.  * @since 
  17.  */  
  18. @SuppressWarnings("unchecked")  
  19. public class JDomDemo {  
  20.     public static void main(String[] args) {  
  21.         String file = "C:\\p.xml"// 文件存放位置  
  22.         JDomDemo dj = new JDomDemo();  
  23.         dj.createXml(file);  
  24.         dj.parserXml(file);  
  25.     }  
  26.     /**   
  27.      * 生成XML   
  28.      * @param filePath 文件路径   
  29.      */  
  30.     public void createXml(String fileName) {  
  31.         Element root = new Element("persons");  
  32.         Document document = new Document(root);  
  33.         Element person = new Element("person");  
  34.         root.addContent(person);  
  35.         Element name = new Element("name");  
  36.         name.setText("java小强");  
  37.         person.addContent(name);  
  38.         Element sex = new Element("sex");  
  39.         sex.setText("man");  
  40.         person.addContent(sex);  
  41.         Element age = new Element("age");  
  42.         age.setText("23");  
  43.         person.addContent(age);  
  44.         XMLOutputter XMLOut = new XMLOutputter();  
  45.         try {  
  46.             Format f = Format.getPrettyFormat();  
  47.             f.setEncoding("UTF-8");//default=UTF-8  
  48.             XMLOut.setFormat(f);  
  49.             XMLOut.output(document, new FileOutputStream(fileName));  
  50.         } catch (Exception e) {  
  51.             e.printStackTrace();  
  52.         }  
  53.     }  
  54.      /**   
  55.      * 解析XML   
  56.      * @param filePath 文件路径   
  57.      */   
  58.     public void parserXml(String fileName) {  
  59.         try {  
  60.             SAXBuilder builder = new SAXBuilder();  
  61.             Document document = builder.build(fileName);  
  62.             Element root = document.getRootElement();  
  63.             List persons = root.getChildren("person");  
  64.             for (int i = 0; i < persons.size(); i++) {  
  65.                 Element person = (Element) persons.get(i);  
  66.                 List pros = person.getChildren();  
  67.                 for (int j = 0; j < pros.size(); j++) {  
  68.                     Element element = (Element) pros.get(j);  
  69.                     System.out.println(element.getName() + ":" + element.getValue());  
  70.                 }  
  71.             }  
  72.         } catch (Exception e) {  
  73.             e.printStackTrace();  
  74.         }  
  75.     }  
  76. }  

 

请您到ITEYE网站看原创,谢谢!

http://cuisuqiang.iteye.com/ ! 

自建博客地址:http://www.javacui.com/ ,内容与ITEYE同步!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值