java 读取xml文件,并转换成字符串

  1. package com.sfzc.books.service.imp;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.io.StringWriter;  
  6. import java.net.URL;  
  7.   
  8.   
  9. import org.jdom.input.SAXBuilder;  
  10. import org.jdom.output.Format;  
  11. import org.jdom.output.XMLOutputter;  
  12.   
  13. import com.sfzc.relation.service.imp.ProPropertiesOperate;  
  14.   
  15. public class GetXml {  
  16.     private String fileUrl=null;  
  17.     public String getFileUrl() {  
  18.         return fileUrl;  
  19.     }  
  20.     public GetXml(String file) {  
  21.         this.fileUrl=GetXml.getPath(file);  
  22.     }  
  23.     /** 
  24.       * 获得项目所在路径 
  25.       * @return String 
  26.       */  
  27.      public static String getPath(String filename) {  
  28.       URL url = GetXml.class.getResource("");  
  29.       File file = new File(url.getFile());  
  30.       String path = file.getParent();  
  31.       while (-1 != path.lastIndexOf("bin") || -1 != path.lastIndexOf(".jar")) {  
  32.        file = new File(path);  
  33.        path = file.getParent();  
  34.       }  
  35.       if (path.startsWith("file")) {  
  36.        path = path.replaceAll("file:""");  
  37.       }  
  38.   
  39.       path = path + File.separator + "imp\\"+filename;   
  40.       path="C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\sfzc\\WEB-INF\\classes\\com\\sfzc\\books\\service\\imp\\"+filename;  
  41.       return path;  
  42.      }  
  43.      public org.jdom.Document load(){  
  44.          org.jdom.Document document=null;  
  45.          String url=this.getFileUrl();  
  46.          try {  
  47.              SAXBuilder reader = new SAXBuilder();   
  48.              document=reader.build(new File(url));  
  49.         } catch (Exception e) {  
  50.              e.printStackTrace();  
  51.         }  
  52.          return document;  
  53.      }  
  54.      public String XmlToString(){  
  55.          org.jdom.Document document=null;  
  56.          document=this.load();  
  57.            
  58.          Format format =Format.getPrettyFormat();      
  59.          format.setEncoding("UTF-8");//设置编码格式   
  60.            
  61.          StringWriter out=null//输出对象  
  62.          String sReturn =""//输出字符串  
  63.          XMLOutputter outputter =new XMLOutputter();   
  64.          out=new StringWriter();   
  65.          try {  
  66.             outputter.output(document,out);  
  67.          } catch (IOException e) {  
  68.             e.printStackTrace();  
  69.          }   
  70.          sReturn=out.toString();   
  71.          return sReturn;  
  72.      }  
  73.   
  74. }  
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. 实例化与调用:  
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. String strXml=null;  
  2. GetXml xml=new GetXml("General.xml");  
  3. strXml=xml.XmlToString();  
  4. return strXml;  
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1.   
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1.   
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值