现在的位置: 首页 > 综合 > 正文

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

2016年12月07日 ⁄ 综合 ⁄ 共 1645字 ⁄ 字号 评论关闭

package com.sfzc.books.service.imp;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URL;


import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import com.sfzc.relation.service.imp.ProPropertiesOperate;

public class GetXml {
	private String fileUrl=null;
	public String getFileUrl() {
		return fileUrl;
	}
	public GetXml(String file) {
		this.fileUrl=GetXml.getPath(file);
	}
	/**
	  * 获得项目所在路径
	  * @return String
	  */
	 public static String getPath(String filename) {
	  URL url = GetXml.class.getResource("");
	  File file = new File(url.getFile());
	  String path = file.getParent();
	  while (-1 != path.lastIndexOf("bin") || -1 != path.lastIndexOf(".jar")) {
	   file = new File(path);
	   path = file.getParent();
	  }
	  if (path.startsWith("file")) {
	   path = path.replaceAll("file:", "");
	  }

	  path = path + File.separator + "imp\\"+filename; 
	  path="C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\sfzc\\WEB-INF\\classes\\com\\sfzc\\books\\service\\imp\\"+filename;
	  return path;
	 }
	 public org.jdom.Document load(){
		 org.jdom.Document document=null;
		 String url=this.getFileUrl();
		 try {
			 SAXBuilder reader = new SAXBuilder(); 
			 document=reader.build(new File(url));
		} catch (Exception e) {
			 e.printStackTrace();
		}
		 return document;
	 }
	 public String XmlToString(){
		 org.jdom.Document document=null;
		 document=this.load();
		 
		 Format format =Format.getPrettyFormat();    
		 format.setEncoding("UTF-8");//设置编码格式 
		 
		 StringWriter out=null; //输出对象
		 String sReturn =""; //输出字符串
		 XMLOutputter outputter =new XMLOutputter(); 
		 out=new StringWriter(); 
		 try {
			outputter.output(document,out);
		 } catch (IOException e) {
			e.printStackTrace();
		 } 
		 sReturn=out.toString(); 
		 return sReturn;
	 }

}
实例化与调用:
       String strXml=null;
       GetXml xml=new GetXml("General.xml");
       strXml=xml.XmlToString();
       return strXml;
 
 

抱歉!评论已关闭.