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


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;
 
 

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
可以使用Java中的DOM和ZipOutputStream来实现将字符串生成xml文件并压缩成zip的功能。以下是一个简单的实现示例: ```java import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class XmlZipper { public static void main(String[] args) throws Exception { // 生成xml字符串 String xmlString = "<root><message>Hello World!</message></root>"; // 将xml字符串转换为Document对象 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new ByteArrayInputStream(xmlString.getBytes())); // 创建zip文件 File zipFile = new File("example.zip"); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); // 将xml写入zip文件 ZipEntry xmlEntry = new ZipEntry("example.xml"); zipOut.putNextEntry(xmlEntry); ByteArrayOutputStream xmlOut = new ByteArrayOutputStream(); document.setXmlStandalone(true); document.normalize(); document.getDocumentElement().normalize(); XmlUtils.writeXml(document, xmlOut); zipOut.write(xmlOut.toByteArray()); xmlOut.close(); zipOut.closeEntry(); // 关闭zip文件 zipOut.finish(); zipOut.close(); } public static class XmlUtils { public static void writeXml(Document document, ByteArrayOutputStream out) throws Exception { javax.xml.transform.TransformerFactory tf = javax.xml.transform.TransformerFactory.newInstance(); javax.xml.transform.Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes"); javax.xml.transform.dom.DOMSource source = new javax.xml.transform.dom.DOMSource(document); javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(out); transformer.transform(source, result); } } } ``` 这段代码将生成一个名为example.zip的压缩文件,并在其中包含了一个名为example.xmlxml文件。你可以使用ZipFile类来解压缩该文件,并读取其中的xml内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值