打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Jdom方式进行的XML文件、Document、String之间的相互转换

XML文件:

 

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?><books><book><name>哈里波特</name><price>10</price><memo>这是一本很好看的书。</memo></book></books>  

Java代码:

 

Java代码  
  1. import java.io.*;  
  2.   
  3. import org.jdom.Document;  
  4. import org.jdom.input.SAXBuilder;  
  5. import org.jdom.output.Format;  
  6. import org.jdom.output.XMLOutputter;  
  7.   
  8. /** 
  9.  * JDOM 方式操作XML 
  10.  *  
  11.  * @author Watson Xu 
  12.  * @date 2011-5-3 下午02:20:49 
  13.  */  
  14. public class OperationXMLByJdom {  
  15.     /** 
  16.      * DOCUMENT格式化输出保存为XML 
  17.      *  
  18.      * @param doc JDOM的Document 
  19.      * @param filePath 输出文件路径 
  20.      * @throws Exception 
  21.      */  
  22.     public static void doc2XML(Document doc, String filePath) throws Exception{  
  23.         Format format = Format.getCompactFormat();   
  24.         format.setEncoding("UTF-8"); //设置XML文件的字符为UTF-8  
  25.         format.setIndent("     ");//设置缩进   
  26.       
  27.         XMLOutputter outputter = new XMLOutputter(format);//定义输出 ,在元素后换行,每一层元素缩排四格   
  28.         FileWriter writer = new FileWriter(filePath);//输出流  
  29.         outputter.output(doc, writer);  
  30.         writer.close();  
  31.     }  
  32.       
  33.     /** 
  34.      * 字符串转换为DOCUMENT 
  35.      *  
  36.      * @param xmlStr 字符串 
  37.      * @return doc JDOM的Document 
  38.      * @throws Exception 
  39.      */  
  40.     public static Document string2Doc(String xmlStr) throws Exception {  
  41.         java.io.Reader in = new StringReader(xmlStr);  
  42.         Document doc = (new SAXBuilder()).build(in);         
  43.         return doc;  
  44.     }  
  45.   
  46.     /** 
  47.      * Document转换为字符串 
  48.      *  
  49.      * @param xmlFilePath XML文件路径 
  50.      * @return xmlStr 字符串 
  51.      * @throws Exception 
  52.      */  
  53.     public static String doc2String(Document doc) throws Exception {  
  54.         Format format = Format.getPrettyFormat();  
  55.         format.setEncoding("UTF-8");// 设置xml文件的字符为UTF-8,解决中文问题  
  56.         XMLOutputter xmlout = new XMLOutputter(format);  
  57.         ByteArrayOutputStream bo = new ByteArrayOutputStream();  
  58.         xmlout.output(doc, bo);  
  59.         return bo.toString();  
  60.     }  
  61.   
  62.     /** 
  63.      * XML转换为Document 
  64.      *  
  65.      * @param xmlFilePath XML文件路径 
  66.      * @return doc Document对象 
  67.      * @throws Exception 
  68.      */  
  69.     public static Document xml2Doc(String xmlFilePath) throws Exception {  
  70.         File file = new File(xmlFilePath);  
  71.         return (new SAXBuilder()).build(file);  
  72.     }  
  73.       
  74.     public static void main(String[] args) {  
  75.         try{  
  76.             Document doc = xml2Doc("test.xml");  
  77.             System.out.println(doc);  
  78.             System.out.println(doc2String(doc));  
  79.             doc = string2Doc(doc2String(doc));  
  80.             doc2XML(doc, "1.xml");  
  81.         } catch (Exception e) {  
  82.             e.printStackTrace();  
  83.         }  
  84.           
  85.     }  
  86. }  

 

输出XML文件:

 

Java代码  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <books>  
  3.      <book>  
  4.           <name>哈里波特</name>  
  5.           <price>10</price>  
  6.           <memo>这是一本很好看的书。</memo>  
  7.      </book>  
  8. </books>  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
string 转化xml xml转化为string
Sring与xml之间转换
用DOM/JDOM解析XML文件(3)
一套JDOM操作XML文件的Base Class
java读写xml文件的方法
JDOM 介绍及使用指南
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服