package com.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
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.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class xmlVSxslTest {
/**
* ����
* @param args
* @throws Exception
*/
public static void main(String[] args)throws Exception{
//获取系统当前时间
SimpleDateFormat currDF=new SimpleDateFormat("yyyyMMddHHmmssSSS");
String fileName=currDF.format(new Date());
String xmlFile = "src/com/test/xml.xml";
String xslFile = "src/com/test/xsl.xsl";
String htmlFileName = "E:/"+fileName+".html";
String xmlFileName = "com/test/test0332xsl.xml";
String xslFileName = "com/test/input0332.xsl";
xmlVSxslTest.Transform(xmlFile, xslFile, htmlFileName);
//����xml�ļ�ͨ��xslģ��ת����Ҫ��xml��ʽ�ļ�
String str = parseXmlResource(xmlFileName,"utf-8");
String src = returnXml(str, xslFileName, "utf-8");
//��src���뵽xml��
strChangeXML(src);
System.out.println(src);
}
/**
* ��xml�ļ�ת��Ϊhtml�ļ�
* @param xmlFileName ת�����ļ���xml�ļ�·��
* @param xslFileName ת����ģ�壬xsl�ļ�·��
* @param htmlFileName �����ļ�·���Լ��ļ�
* @throws Exception
*/
public static void Transform(String xmlFileName, String xslFileName,String htmlFileName)throws Exception {
TransformerFactory tFac = TransformerFactory.newInstance();
Source xslSource = new StreamSource(xslFileName);
Transformer t = tFac.newTransformer(xslSource);
File xmlFile = new File(xmlFileName);
File htmlFile = new File(htmlFileName);
Source source = new StreamSource(xmlFile);
Result result = new StreamResult(htmlFile);
t.transform(source, result);
}
/**
* ���xml�ļ�
* @param resource xml����·��
* @param encoding �ַ����
* @return String ������Ҫ��xml�ַ�
* @throws Exception
*/
public static String parseXmlResource(String resource,String encoding)throws Exception{
//��ָ�����еļ������еõ��������ж�ȡָ����Դ
InputStream inputStream = xmlVSxslTest.class.getClassLoader().getResourceAsStream(resource);
//�õ�һ���µ�����Դ���ֽ���
InputSource inputSource = new InputSource(inputStream);
//�����ַ����
inputSource.setEncoding(encoding);
//���幤��API ��ʹӦ�ó����ô�XML�ļ����DOM�������Ľ�������
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//����һ����ʵ����ĵ������ʹ�õ�ǰ���õIJ���
DocumentBuilder builder = factory.newDocumentBuilder();
//�ڸ������Դ�����ݽ���Ϊһ��XML�ĵ�������һ���µ�DOM �ļ�����
Document document = builder.parse(inputSource);
//���һ��TransformerFactory����ʵ��
TransformerFactory tf = TransformerFactory.newInstance();
//����һ���µı�ѹ����ִ�и��Ƶ�Դ����Ľ��
Transformer t = tf.newTransformer();
//�����������
t.setOutputProperty("encoding", encoding);
//����һ���µ��ֽ����������
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//�ؼ���� document.getDocumentElement()��Ϊ�˲�����ɵ�xml����ͷ������
//����һ���µ�DOM�ڵ������Դ
//�ĵ�Ԫ�ص��ӽڵ������
//���ֽ���������һ��StreamResult
t.transform(new DOMSource(document.getDocumentElement()), new StreamResult(bos));
//�������������ת��Ϊ�ַ����ָ�����ַ���뽫�ֽ�ת�����ַ�
String xml = bos.toString(encoding);
return xml ;
}
/**
* ת���ļ���xmlͨ��xslת����
* @param xml ת���ļ�
* @param xsl ת��ģ��
* @param encoding �ַ����
* @return String ����ת�����xml�ַ�
* @throws Exception
*/
public static String returnXml(String xml,String xsl,String encoding)throws Exception{
//��ȡ�ַ�������
StringWriter stringWriter = new StringWriter();
//��ȡ��ӡ����������������Ϊ�ַ�����ʽ
PrintWriter printWriter = new PrintWriter(stringWriter);
//��������String,��ȡXML�ַ�������Դ
Source srcSource = new StreamSource(new ByteArrayInputStream(xml.getBytes(encoding)));
//����ת��������Ϊ��ӡ��
Result destResult = new StreamResult(printWriter);
//��ȡת��ģ��
ClassLoader cl = xmlVSxslTest.class.getClassLoader();
InputStream is = cl.getResourceAsStream(xsl);
Source xslSource = new StreamSource(is);
//����ת������
TransformerFactory transFact = TransformerFactory.newInstance();
//����ת������
Transformer trans = transFact.newTransformer(xslSource);
//ʵ��ת��
trans.transform(srcSource, destResult);
//��ת�����ֵ�� ���ص��ַ���
String xmlParsed = stringWriter.toString();
//�رմ�ӡ��
printWriter.close();
return xmlParsed ;
}
//���ַ�string����ת����xml�ļ�
public static void strChangeXML(String str) throws IOException {
SimpleDateFormat currDF=new SimpleDateFormat("yyyyMMddHHmmssSSS");
String fileName=currDF.format(new Date());
SAXReader saxReader = new SAXReader();
org.dom4j.Document document;
try {
document = saxReader.read(new ByteArrayInputStream(str.getBytes("UTF-8")));
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileWriter(new File("src/com/test/"+fileName+".xml")),format);
writer.write(document);
writer.close();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
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.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class xmlVSxslTest {
/**
* ����
* @param args
* @throws Exception
*/
public static void main(String[] args)throws Exception{
//获取系统当前时间
SimpleDateFormat currDF=new SimpleDateFormat("yyyyMMddHHmmssSSS");
String fileName=currDF.format(new Date());
String xmlFile = "src/com/test/xml.xml";
String xslFile = "src/com/test/xsl.xsl";
String htmlFileName = "E:/"+fileName+".html";
String xmlFileName = "com/test/test0332xsl.xml";
String xslFileName = "com/test/input0332.xsl";
xmlVSxslTest.Transform(xmlFile, xslFile, htmlFileName);
//����xml�ļ�ͨ��xslģ��ת����Ҫ��xml��ʽ�ļ�
String str = parseXmlResource(xmlFileName,"utf-8");
String src = returnXml(str, xslFileName, "utf-8");
//��src���뵽xml��
strChangeXML(src);
System.out.println(src);
}
/**
* ��xml�ļ�ת��Ϊhtml�ļ�
* @param xmlFileName ת�����ļ���xml�ļ�·��
* @param xslFileName ת����ģ�壬xsl�ļ�·��
* @param htmlFileName �����ļ�·���Լ��ļ�
* @throws Exception
*/
public static void Transform(String xmlFileName, String xslFileName,String htmlFileName)throws Exception {
TransformerFactory tFac = TransformerFactory.newInstance();
Source xslSource = new StreamSource(xslFileName);
Transformer t = tFac.newTransformer(xslSource);
File xmlFile = new File(xmlFileName);
File htmlFile = new File(htmlFileName);
Source source = new StreamSource(xmlFile);
Result result = new StreamResult(htmlFile);
t.transform(source, result);
}
/**
* ���xml�ļ�
* @param resource xml����·��
* @param encoding �ַ����
* @return String ������Ҫ��xml�ַ�
* @throws Exception
*/
public static String parseXmlResource(String resource,String encoding)throws Exception{
//��ָ�����еļ������еõ��������ж�ȡָ����Դ
InputStream inputStream = xmlVSxslTest.class.getClassLoader().getResourceAsStream(resource);
//�õ�һ���µ�����Դ���ֽ���
InputSource inputSource = new InputSource(inputStream);
//�����ַ����
inputSource.setEncoding(encoding);
//���幤��API ��ʹӦ�ó����ô�XML�ļ����DOM�������Ľ�������
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//����һ����ʵ����ĵ������ʹ�õ�ǰ���õIJ���
DocumentBuilder builder = factory.newDocumentBuilder();
//�ڸ������Դ�����ݽ���Ϊһ��XML�ĵ�������һ���µ�DOM �ļ�����
Document document = builder.parse(inputSource);
//���һ��TransformerFactory����ʵ��
TransformerFactory tf = TransformerFactory.newInstance();
//����һ���µı�ѹ����ִ�и��Ƶ�Դ����Ľ��
Transformer t = tf.newTransformer();
//�����������
t.setOutputProperty("encoding", encoding);
//����һ���µ��ֽ����������
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//�ؼ���� document.getDocumentElement()��Ϊ�˲�����ɵ�xml����ͷ������
//����һ���µ�DOM�ڵ������Դ
//�ĵ�Ԫ�ص��ӽڵ������
//���ֽ���������һ��StreamResult
t.transform(new DOMSource(document.getDocumentElement()), new StreamResult(bos));
//�������������ת��Ϊ�ַ����ָ�����ַ���뽫�ֽ�ת�����ַ�
String xml = bos.toString(encoding);
return xml ;
}
/**
* ת���ļ���xmlͨ��xslת����
* @param xml ת���ļ�
* @param xsl ת��ģ��
* @param encoding �ַ����
* @return String ����ת�����xml�ַ�
* @throws Exception
*/
public static String returnXml(String xml,String xsl,String encoding)throws Exception{
//��ȡ�ַ�������
StringWriter stringWriter = new StringWriter();
//��ȡ��ӡ����������������Ϊ�ַ�����ʽ
PrintWriter printWriter = new PrintWriter(stringWriter);
//��������String,��ȡXML�ַ�������Դ
Source srcSource = new StreamSource(new ByteArrayInputStream(xml.getBytes(encoding)));
//����ת��������Ϊ��ӡ��
Result destResult = new StreamResult(printWriter);
//��ȡת��ģ��
ClassLoader cl = xmlVSxslTest.class.getClassLoader();
InputStream is = cl.getResourceAsStream(xsl);
Source xslSource = new StreamSource(is);
//����ת������
TransformerFactory transFact = TransformerFactory.newInstance();
//����ת������
Transformer trans = transFact.newTransformer(xslSource);
//ʵ��ת��
trans.transform(srcSource, destResult);
//��ת�����ֵ�� ���ص��ַ���
String xmlParsed = stringWriter.toString();
//�رմ�ӡ��
printWriter.close();
return xmlParsed ;
}
//���ַ�string����ת����xml�ļ�
public static void strChangeXML(String str) throws IOException {
SimpleDateFormat currDF=new SimpleDateFormat("yyyyMMddHHmmssSSS");
String fileName=currDF.format(new Date());
SAXReader saxReader = new SAXReader();
org.dom4j.Document document;
try {
document = saxReader.read(new ByteArrayInputStream(str.getBytes("UTF-8")));
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileWriter(new File("src/com/test/"+fileName+".xml")),format);
writer.write(document);
writer.close();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}