1个回答
2016-05-08 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
java中将string转换成xml文件,使用开源jar包 dom4j:
package com.webdesk.swing.powertable.util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class XmlUtil {
public static String xmlChangeString(String fileName){
try {
SAXReader saxReader = new SAXReader();//新建一个解析类
Document tempDocument = saxReader.read(XmlUtil.class.getClassLoader().getResourceAsStream(fileName));//读入一个文件
return tempDocument.asXML();
} catch (DocumentException e) {
e.printStackTrace();
}
return null;
}
//将字符串string类型转换成xml文件
public static void strChangeXML(String str) throws IOException {
SAXReader saxReader = new SAXReader();
Document document;
try {
document = saxReader.read(new ByteArrayInputStream(str.getBytes("UTF-8")));
OutputFormat format = OutputFormat.createPrettyPrint();
/** 将document中的内容写入文件中 */
XMLWriter writer = new XMLWriter(new FileWriter(new File("src/com/webdesk/swing/powertable/digester/cctv.xml")),format);
writer.write(document);
writer.close();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
package com.webdesk.swing.powertable.util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class XmlUtil {
public static String xmlChangeString(String fileName){
try {
SAXReader saxReader = new SAXReader();//新建一个解析类
Document tempDocument = saxReader.read(XmlUtil.class.getClassLoader().getResourceAsStream(fileName));//读入一个文件
return tempDocument.asXML();
} catch (DocumentException e) {
e.printStackTrace();
}
return null;
}
//将字符串string类型转换成xml文件
public static void strChangeXML(String str) throws IOException {
SAXReader saxReader = new SAXReader();
Document document;
try {
document = saxReader.read(new ByteArrayInputStream(str.getBytes("UTF-8")));
OutputFormat format = OutputFormat.createPrettyPrint();
/** 将document中的内容写入文件中 */
XMLWriter writer = new XMLWriter(new FileWriter(new File("src/com/webdesk/swing/powertable/digester/cctv.xml")),format);
writer.write(document);
writer.close();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询