java中使用xls格式化xml的实例
程序员文章站
2023-12-12 08:52:58
在java中调用xls格式化xml
使用javax.xml.transform.transformer,将xml用xls格式化为另一种xml
输入为xml字...
在java中调用xls格式化xml
使用javax.xml.transform.transformer,将xml用xls格式化为另一种xml
输入为xml字符串,输出也为xml字符串,代码模板如下:
java代码
//这里使用xml-apis.jar实现javax.xml.transform.transformer import javax.xml.transform.transformer; import javax.xml.transform.stream.streamresult; import javax.xml.transform.stream.streamsource; public class transformxmlbyxls{ private transformer transformer; public string handlexml(){ string sourcexml="..."; stringwriter sw = new stringwriter(); inputstream in= new bytearrayinputstream(sourcexml .getbytes("utf-8")); transformerxslimpl(...,"utf-8"); transformer.transform(in, sw); string targetxml = sw.tostring(); return targetxml; } private transformerxslimpl(file xsltemplatefile, string encoding) throws exception { inittransformerfactory(); transformer = getxsltransformertemplate(xsltemplatefile); transformer.setoutputproperty(outputkeys.encoding, encoding); } private transformer getxsltransformertemplate(file xslfile) throws transformerfactoryconfigurationerror, transformerconfigurationexception { transformerfactory tfactory = transformerfactory.newinstance(); templates translet = tfactory.newtemplates(new streamsource(xslfile)); return translet.newtransformer(); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!