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. python Django 学习笔记(六)—— 写一个简单blog做增删改练手

    简单效果图 1,创建一个项目myblog 可参考这里 myblog/ manage.py myblog/ __init__.py settings.py urls.py wsgi.py 2,创建blo ...

  2. WPF优化体验<一>

    最近将一个开发和维护了五年的一个Winform项目进行重构,考虑到最近很流行将用户体验挂在嘴上,于是采用了WPF技术,希望能在外观和体验上有一个全新的效果. 以前使用Winform的时候内存控制得不错 ...

  3. C#判断ip地址是否ping的通

    Ping pingSender = new Ping(); PingReply reply = pingSender.Send("127.0.0.1",120);//第一个参数为i ...

  4. Extjs 下拉框下拉选项为Object object

    使用Extjs的下拉框出现下拉选项为Object object的问题. 原因在于对store属性提供的是data信息,而不是store对象

  5. c/c++常用代码--udp多播

    #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <winsock.h ...

  6. jquery页面刷新reload

    今天知道了jquery页面刷新的一个方法,很简单的一句: window.location.reload()刷新当前页面,不得不说我jquery学的很糟糕,明明都学过,但是等用到的时候就想不起来了.不过 ...

  7. 文件读写 swift

    // // ViewController.swift // 文件读写 // // Created by mac on 15/7/12. // Copyright (c) 2015年 fangyuhao ...

  8. objective-c自学总结(一)---面向对象

    本人大二本科在读,利用一个月多一点的时间对OC语言基础进行了自学,在下一阶段UI学习开始之前, 对这一阶段的自学进行一些总结.在此特别感谢刘晓斌学长和无线互联3G学院 首先说一下对OC的整体感觉,这是 ...

  9. 使用Log4j进行日志操作

    使用Log4j进行日志操作 一.Log4j简介 (1)概述 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接字服 ...

  10. LeetCode Shell Problems

    195. Tenth Line -- 第十行 How would you print just the 10th line of a file? Solution: awk 'NR==10' file ...