利用XSLT转换XML学习笔记

import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

public class Transform {

    /**
     * Performes an XSLT transformation, sending the results
     * to System.out.
     */
    public static void main(String[] args) throws Exception {
//        if (args.length != 2) {
//            System.err.println(
//                "Usage: java Transform [xmlfile] [xsltfile]");
//            System.exit(1);
//        }

        File xmlFile = new File("testdb1.xml");
        File xsltFile = new File("MapTomytest.xslt");

        Source xmlSource = new StreamSource(xmlFile);
        Source xsltSource = new StreamSource(xsltFile);

        TransformerFactory transFact =
                TransformerFactory.newInstance();
//       加载进缓存 这样提高效率
        Templates cachedXSLT = transFact.newTemplates(xsltSource);
        Transformer trans = cachedXSLT.newTransformer();

        StreamResult result = new StreamResult(new java.io.File("outputtest.xml"));
        trans.transform(xmlSource,result);

    }
}

你可能感兴趣的:(学习笔记)