我通过将 XML/HTML 文件 (temp.xml) 和 XSLT(temp.xsl) 作为输入来生成 XML 文件,并且我的输出生成为具有新名称 (temp_copy.xml) 的单独文件,但我想覆盖输入 XML 文件,而不是创建新文件。我尝试提供与输入文件中相同的路径,但这不起作用。那么实现这一目标的另一种方法是什么?提前致谢。
我的Java代码:
public class SimpleXSLT {
public static void main(String[] args) {
String inXML = "C:/tmp/temp.xml";
String inXSL = "C:/tmp/temp.xsl";
String outTXT = "C:/tmp/temp_copy.xml";
SimpleXSLT st = new SimpleXSLT();
try {
st.transform(inXML,inXSL,outTXT);
} catch(TransformerConfigurationException e) {
System.err.println("Invalid factory configuration");
System.err.println(e);
} catch(TransformerException e) {
System.err.println("Error during transformation");
System.err.println(e);
}
}
public void transform(String inXML,String inXSL,String outTXT)
throws TransformerConfigurationException,
TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource xslStream = new StreamSource(inXSL);
Transformer transformer = factory.newTransformer(xslStream);
transformer.setErrorListener(new MyErrorListener());
StreamSource in = new StreamSource(inXML);
StreamResult out = new StreamResult(outTXT);
transformer.transform(in,out);
System.out.println("The generated XML file is:" + outTXT);
}
}