packageblog.youkuaiyun.com.arui;

importjava.io.*;
importjavax.xml.transform.*;
importjavax.xml.transform.stream.*;


publicclassBasicXsl...{

/***//**
*Thismethodappliesthexslfiletoinfile,andwritestheoutputtoout
*file.
*
*@paraminFilename
*infilepath
*@paramoutFilename
*outfilepath
*@paramxslFilename
*xslfilepath
*/
publicstaticvoidxsl(
StringinFilename,
StringoutFilename,
StringxslFilename)...{
try...{
//Createtransformerfactory
TransformerFactoryfactory=TransformerFactory.newInstance();
//Usethefactorytocreateatemplatecontainingthexslfile
Templatestemplate=factory.newTemplates(newStreamSource(
newFileInputStream(xslFilename)));
//Usethetemplatetocreateatransformer
Transformerxformer=template.newTransformer();
//Preparetheinputandoutputfiles
Sourcesource=newStreamSource(newFileInputStream(inFilename));
Resultresult=newStreamResult(newFileOutputStream(outFilename));
//Applythexslfiletothesourcefileandwritetheresulttothe
//outputfile
xformer.transform(source,result);
}catch(FileNotFoundExceptione)...{
//Filenotfound
}catch(TransformerConfigurationExceptione)...{
//AnerroroccurredintheXSLfile
}catch(TransformerExceptione)...{
//AnerroroccurredwhileapplyingtheXSLfile
//Getlocationoferrorininputfile
}
}
}
Java XSLT 转换示例
本文介绍了一个使用 Java 进行 XML 文件转换的例子。通过 TransformerFactory 和 StreamSource 类来应用 XSL 文件对输入 XML 文件进行转换,并将结果输出到另一个文件中。此过程包括了错误处理,确保转换过程中遇到的问题可以被正确捕捉。
1129

被折叠的 条评论
为什么被折叠?



