jdom处理的XML Document 和String 之间的相互转化
package util; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.StringReader; import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter; /**
* JDOM 方式操作XML
*
* @author Watson Xu
* @date 2011-5-3 下午02:20:49
*/
public class OperationXMLByJdom {
/**
* DOCUMENT格式化输出保存为XML
*
* @param doc JDOM的Document
* @param filePath 输出文件路径
* @throws Exception
*/
public static void doc2XML(Document doc, String filePath) throws Exception{
Format format = Format.getCompactFormat();
format.setEncoding("UTF-8"); //设置XML文件的字符为UTF-8
format.setIndent(" ");//设置缩进 XMLOutputter outputter = new XMLOutputter(format);//定义输出 ,在元素后换行,每一层元素缩排四格
FileWriter writer = new FileWriter(filePath);//输出流
outputter.output(doc, writer);
writer.close();
} /**
* 字符串转换为DOCUMENT
*
* @param xmlStr 字符串
* @return doc JDOM的Document
* @throws Exception
*/
public static Document string2Doc(String xmlStr) throws Exception {
java.io.Reader in = new StringReader(xmlStr);
Document doc = (new SAXBuilder()).build(in);
return doc;
} /**
* Document转换为字符串
*
* @param xmlFilePath XML文件路径
* @return xmlStr 字符串
* @throws Exception
*/
public static String doc2String(Document doc) throws Exception {
Format format = Format.getPrettyFormat();
format.setEncoding("UTF-8");// 设置xml文件的字符为UTF-8,解决中文问题
XMLOutputter xmlout = new XMLOutputter(format);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
xmlout.output(doc, bo);
return bo.toString();
} /**
* XML转换为Document
*
* @param xmlFilePath XML文件路径
* @return doc Document对象
* @throws Exception
*/
public static Document xml2Doc(String xmlFilePath) throws Exception {
File file = new File(xmlFilePath);
return (new SAXBuilder()).build(file);
} public static void main(String[] args) {
try{
Document doc = xml2Doc("test.xml");
System.out.println(doc);
System.out.println(doc2String(doc));
doc = string2Doc(doc2String(doc));
doc2XML(doc, "1.xml");
} catch (Exception e) {
e.printStackTrace();
} }
}
jdom处理的XML Document 和String 之间的相互转化的更多相关文章
- xml字符串,xml对象,数组之间的相互转化
<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller { pu ...
- 字符串 字符数组, pcha string 之间的相互转化, 很重要。 很蛋疼
http://www.cnblogs.com/del88/p/5448981.html Delphi字符串.PChar与字符数组之间的转换 来自:http://my.oschina.net/kaven ...
- Spring相关:jdom学习:读取xml文件
云课堂马士兵的spring2.5课程中提到的 用JDOM读取XML文件需先用org.jdom.input.SAXBuilder对象的build()方法创建Document对象,然后用Document类 ...
- jdom学习读取XML文件
用JDOM读取XML文件需先用org.jdom.input.SAXBuilder对象的build()方法创建Document对象,然后用Document类.Element类等的方法读取所需的内容.IB ...
- jdom dom4j解析xml不对dtd doctype进行验证(转)
一.写在所有之前:因为dom4j和jdom在这个问题上处理的方法是一模一样的,只是一个是SAXBuilder 一个SAXReader,这里以jdom距离,至于dom4j只需要同理替换一下就可以了.二. ...
- string 转化xml && xml转化为string
一.使用最原始的javax.xml.parsers,标准的jdk api // 字符串转XML [java] view plaincopyprint? String xmlStr = \". ...
- Modifying namespace in XML document programmatically
Modifying namespace in XML document programmatically static XElement stripNS(XElement root) { return ...
- [翻译][Nokogiri官方教程] 解析HTML/XML文档 / Parsing an HTML/XML Document
From a String From a File From the Internet Parse Options Encoding 原文: Parsing an HTML/XML Document ...
- parsing XML document from class path resource
遇到问题:parsing XML document from class path resource [spring/resources] 解决方法:项目properties— source—remo ...
随机推荐
- 关于activity_main.xml与fragment_main.xml
第一种解决办法 新版安装SDK文件一开始有两个XML文件,activity_main.xml和fragment_main.xml,不习惯的可以这样处理:1.删除fragment_main.xml整个文 ...
- DB2批处理数据导入
这里需要两个BAT文件 first.bat @echo off @set /p databaseName=1)请输入数据库名: @set /p userName=2)请输入用户名: @set /p u ...
- SpringMvc中Interceptor拦截器用法
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆等. 一. 使用场景 1 ...
- 机器学习相关——协同过滤
在现今的推荐技术和算法中,最被大家广泛认可和采用的就是基于协同过滤的推荐方法.本文将带你深入了解协同过滤的秘密.下面直接进入正题 1 什么是协同过滤 协同过滤是利用集体智慧的一个典型方法.要理解什么是 ...
- hdu 5183 Negative and Positive (NP)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5183 Negative and Positive (NP) Description When give ...
- php连接mysql报错No such file or directory
php测试文件如下: 1 2 3 4 5 6 7 8 9 10 11 <?php $con = mysql_connect("localhost","root&qu ...
- C#取枚举描述
一直都觉得枚举是个很不错的东西,可以给我们带来很多方便,而且也增加代码的可读性. 我在之前已经介绍过枚举的简要应用了,再次再来写下怎么获取枚举的描述. 源码如下: 首先,我们定义个含有描述的枚举类型 ...
- Hibernate使用原生sql语句
异常类型1:could not extract resulted --->表名字和类名不一致 异常类型2:could not execute query---->可能是sql语句错: 异常 ...
- android 开发-自定义多节点进度条显示
看效果图: 里面的线段颜色和节点图标都是可以自定义的. main.xml <RelativeLayout xmlns:android="http://schemas.android.c ...
- 配置Symfony2
安装成功后打开server php app/console server:run 127.0.0.1:8000 然后在浏览器输入localhost/8000/config.php进行配置 1.date ...