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 之间的相互转化的更多相关文章

  1. xml字符串,xml对象,数组之间的相互转化

    <?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller { pu ...

  2. 字符串 字符数组, pcha string 之间的相互转化, 很重要。 很蛋疼

    http://www.cnblogs.com/del88/p/5448981.html Delphi字符串.PChar与字符数组之间的转换 来自:http://my.oschina.net/kaven ...

  3. Spring相关:jdom学习:读取xml文件

    云课堂马士兵的spring2.5课程中提到的 用JDOM读取XML文件需先用org.jdom.input.SAXBuilder对象的build()方法创建Document对象,然后用Document类 ...

  4. jdom学习读取XML文件

    用JDOM读取XML文件需先用org.jdom.input.SAXBuilder对象的build()方法创建Document对象,然后用Document类.Element类等的方法读取所需的内容.IB ...

  5. jdom dom4j解析xml不对dtd doctype进行验证(转)

    一.写在所有之前:因为dom4j和jdom在这个问题上处理的方法是一模一样的,只是一个是SAXBuilder 一个SAXReader,这里以jdom距离,至于dom4j只需要同理替换一下就可以了.二. ...

  6. string 转化xml && xml转化为string

    一.使用最原始的javax.xml.parsers,标准的jdk api // 字符串转XML [java] view plaincopyprint? String xmlStr = \". ...

  7. Modifying namespace in XML document programmatically

    Modifying namespace in XML document programmatically static XElement stripNS(XElement root) { return ...

  8. [翻译][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 ...

  9. parsing XML document from class path resource

    遇到问题:parsing XML document from class path resource [spring/resources] 解决方法:项目properties— source—remo ...

随机推荐

  1. Vim,一个开放源代码的文本编辑器(转)

    Vim,http://linux.21ds.net/2002/03/13/0268dc26fd9c725c23dae68d797935f3/ 作者:Bram Moolenaar 翻译:slimzhao ...

  2. wpf的datepicker处理

    如果有2个datepicker,控制时间起和止的话,可以把第二个datepicker加一个属性,DisplayDateStart = "{Binding SelectedDate,Eleme ...

  3. [原创]Postgres-XC集群笔记-概念与环境搭建

    文所描述的Postgres-XC版本:v1.2.1项目主页地址:http://sourceforge.net/projects/postgres-xc/ pdf文件下载: Postgres-XC集群搭 ...

  4. Fragment+RadioButton实现点击切换页面效果

    首先我们需要在主布局文件中 放一个 容器,方便让fragment加入进去,我们创建了四个Fragment,并用RedioButton实现了导航栏 MainActivity.java package c ...

  5. umask设置导致的weblogic中的应用上传的文件没有权限打开

    去年,在公司的某一weblogic上部署的web应用上传文件后却没有读的权限.因为weblogic在Linux上部署,上传文件是mount到了一台安装了NFS的Windows Server上. 当时本 ...

  6. android开发系列之回调函数

    想必对于回调函数大家肯定不陌生,因为这是我们开发里面常用的代码技巧.我也就不废话了,让我们直接来看代码吧! public class TestCallback { public interface I ...

  7. Question about pairing/bonding?

    Except that on android you can bypass the pairing dialog if you know the PIN in advance through a di ...

  8. 3-附1 ->和*的区别

    问题: c++ .和 ->有什么区别? 还有什么是继承什么是派生?-------------------------------------------------------------- 比 ...

  9. 55.ERROR:Place:1136 - This design contains a global buffer instance…… non-clock load pins off chip

    ISE在布局布线时,出现下图所示错误. 对于"clock_dedicated_route”错误原因有两种情况: 1.  就是有一个时钟你没有放到全局时钟或者局部时钟的引脚,布局的时候不能把它 ...

  10. Haskell 趣学指南 入门笔记(二)

    显示类型声明,Haskell是不用定义类型的原因,很像python 想要确定某个表达式的类型 *Main> :t 'a' 'a' :: Char *Main> :t True True : ...