首页>代码>js+DOM转化成html表格,并且实现强大的分页功能>/Dom2Table/src/com/zhangjun/xml/TransXML.java
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";
  }
}
最近下载更多
2002-GRIT  LV2 2023年5月19日
aslyyds  LV2 2023年1月28日
wsir001  LV1 2022年5月24日
joker  LV1 2021年4月22日
liuzi919  LV1 2019年12月19日
myjzb111  LV9 2019年12月2日
1292992465  LV1 2019年11月9日
xuyongff  LV24 2019年11月4日
4968111  LV8 2019年10月22日
Remtarose  LV1 2019年9月30日
最近浏览更多
WHY-small  LV15 2023年12月29日
ycmyyt  LV2 2023年6月6日
qq1154180193 2023年5月21日
暂无贡献等级
2002-GRIT  LV2 2023年5月19日
月光skr  LV3 2023年4月9日
Gin19960217  LV4 2023年3月28日
aslyyds  LV2 2023年1月28日
微信网友_6270418622812160  LV3 2022年12月21日
呵呵哈哈哈  LV10 2022年9月25日
007dog  LV5 2022年6月6日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友