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;
}
}
相关文档:
一、WDDX的产生
WDDX,英文全称为Web Distributed Data
Exchange,是一种基于XML的Web分布式数据交换技术。WDDX最早是美国Allaire公司的程序技术设计师Simeon
Simeonov为了解决ColdFusion中涉及到的分布计算问题而建立的。随着工作的开展,WDDX逐渐演变成为一种可用于不同的应用环境中交换复杂的结构 ......
转:http://hi.baidu.com/oneshotonekill/blog/item/be68b513f7c929d7f6039e1e.html
Whitespace is not allowed before an XML Processing Instruction (< ? ... ?>). HTMLComponent.Eclipse在编辑mxml的时候提示这样的错误。检查才发现代码中在<? ?>之前存在空格。 ......
得到一个需要处理的XMl
private string GetSaveItem()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<menuCollection/>");
foreach (TreeNode node in trvAccessRight.CheckedNodes)
{
if (node != trvAccess ......
用java创建Xml的4大类:
Element:节点类
Attribute属性类
Document:指的就是文档类
XMLOutput:输出类
此类是用java建立一个xml文件
public class TestJdom {
//创建XML(模型)dom
public static void main(String[] args) {
......
XStream使用
XStream是一个Java对象和XML相互转换的工具,很好很强大。提供了所有的基础类型、数组、集合等类型直接转换的支持。因此XML常用于数据交换、对象序列化(这种序列化和Java对象的序列化技术有着本质的区别)。
XStream中的核心类就是XStream类,一般来说,熟悉这个类基本就够用了,如果你用的更多,估计是� ......