XML格式转化工具类
基于dom4j的XML格式转化类
package com.lixi.util;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
* <p>Title: </p>
* <p>Description: XML格式转化工具</p>
* <p>Copyright: Copyright (c) 2010-02-05</p>
* <p>Company: </p>
* @author li.xi
* @version 1.0
*/
public class XmlHelper {
public XmlHelper() {
}
/**
* String格式的XML转Document
* @param xml
* @param charSet 字符集编码设置 如:GBK
* @return Document
* @throws Exception
*/
public static Document buildDoc(String xml, String charSet)
throws Exception {
InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
SAXReader reader = new SAXReader();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, charSet);
Document document = reader.read(inputStreamReader);
inputStreamReader.close();
return document;
}
/**
* Document格式的XML转String
* @param document
* @param charSet 字符集编码设置
* @return String
* @throws Exception
*/
public static String setCharSet(Document document, String charSet)
throws Exception {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding(charSet);
ByteArrayOutputStream fos = new ByteArrayOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos,
charSet));
XMLWriter writer = new XMLWriter(bw, format);
writer.write(document);
bw.close();
String restr = fos.toString();
fos.close();
return restr;
}
}
相关文档:
1. 基础:对XML进行简单的查询, 插入, 删除, 编辑操作
在数据量不是很大的情况下,可以用xml代替数据库存储数据,但是要定义xsd文件来验证xml文件,保证xml数据格式。
以下网页介绍了如何利用C#实现xml的查询,插入,删除和更新操作,这表明xml可以代替数据库存储一定量的数据。
http://blog.csdn.net/cmoonc/archive/20 ......
If XML data in the table is less than 32K for each record, then you can directly unload the data as char. If XML data exceeds 32K for some records, then you have to unload the common data and the XML data separately. First, create a template for unloading XML into a PDS: TEMPLATE LOBFRV DSN 'AAA. ......
转:http://hi.baidu.com/oneshotonekill/blog/item/be68b513f7c929d7f6039e1e.html
Whitespace is not allowed before an XML Processing Instruction (< ? ... ?>). HTMLComponent.Eclipse在编辑mxml的时候提示这样的错误。检查才发现代码中在<? ?>之前存在空格。 ......
1. 下载与安装
dom4j是sourceforge.net上的一个开源项目,主要用于对XML的解析。从2001年7月发布第一版以来,已陆续推出多个版本,目前最高版本为1.5。
dom4j专门针对Java开发,使用起来非常简单、直观, ......
用java创建Xml的4大类:
Element:节点类
Attribute属性类
Document:指的就是文档类
XMLOutput:输出类
此类是用java建立一个xml文件
public class TestJdom {
//创建XML(模型)dom
public static void main(String[] args) {
  ......