package com.zhangjun.xml;
import java.io.PrintStream;
import java.io.StringWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
public class TransXML
{
public String trans(String xslname, String xmlname, Hashtable hs)
{
StreamSource xsl = new StreamSource(xslname);
StreamSource xmlData = new StreamSource(xmlname);
return go_trans(xmlData, xsl, hs);
}
public String trans(String xslname, Document dom, Hashtable hs)
{
StreamSource xsl = new StreamSource(xslname);
DOMSource xmlData = new DOMSource(dom);
return go_trans(xmlData, xsl, hs);
}
private String go_trans(Source xmlSource, Source xslSource, Hashtable hs)
{
try
{
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
dFactory.setNamespaceAware(true);
DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xslSource);
StringWriter wt = new StringWriter();
StreamResult streamResult = new StreamResult();
streamResult.setWriter(wt);
transformer.setOutputProperty("encoding", "GB2312");
Enumeration hs_value = hs.elements();
for (Enumeration hs_name = hs.keys(); hs_name.hasMoreElements(); )
{
String para_name = (String)hs_name.nextElement();
String para_value = (String)hs_value.nextElement();
if (para_value != null) transformer.setParameter(para_name, para_value);
}
transformer.transform(xmlSource, streamResult);
return wt.toString();
}
catch (Exception e)
{
System.out.println("trans error:" + e);
e.printStackTrace(); }
return "error";
}
}
最近下载更多
最近浏览更多
3334004690 LV11
2024年5月27日
WHY-small LV15
2023年12月29日
ycmyyt LV2
2023年6月6日
qq1154180193
2023年5月21日
暂无贡献等级
2002-GRIT LV2
2023年5月19日
月光skr LV4
2023年4月9日
Gin19960217 LV4
2023年3月28日
aslyyds LV2
2023年1月28日
微信网友_6270418622812160 LV3
2022年12月21日
呵呵哈哈哈 LV10
2022年9月25日

