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;
}
}
相关文档:
一、XML只有一个Table的情况
(1)userInfo.xml
<?xml version="1.0" encoding="utf-8" ?>
<UserInfo ......
一、WDDX的产生
WDDX,英文全称为Web Distributed Data
Exchange,是一种基于XML的Web分布式数据交换技术。WDDX最早是美国Allaire公司的程序技术设计师Simeon
Simeonov为了解决ColdFusion中涉及到的分布计算问题而建立的。随着工作的开展,WDDX逐渐演变成为一种可用于不同的应用环境中交换复杂的结构 ......
得到一个需要处理的XMl
private string GetSaveItem()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<menuCollection/>");
foreach (TreeNode node in trvAccessRight.CheckedNodes)
{
if (node != trvAccess ......
DECLARE @x xml
SET @x='
<root>
<ShopAccount>
<ActivityType>IA - PM Standing WO (for LPI report)</ActivityType>
<ProjectNo>R</ProjectNo>
</ShopAccount>
<ShopAccount>
......
测试用的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<schools>
<school id='111'>测试学校</school>
<school id='222'>测试学校22
<class id='2.1'>测试班级222</class>
</school>
</schools>
测试用的JavaScript代码
$().ready(function ......