1

I'm generating XML file by taking XML/HTML file (temp.xml) and XSLT(temp.xsl) as input and my output is generated as a separate file with the new name(temp_copy.xml) but I want to overwrite the input XML file instead of creating a new file. I tried by giving the same path as it was in the input file but that didn't work. So what can be the other way to achieve this? Thanks in advance.

My Java code:

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);
    }
}
RahulD
  • 709
  • 2
  • 16
  • 39

2 Answers2

2

"But that didn't work" needs to be better defined. You got an error? If so, what did it say? If not, what happened that was contrary to your expectation?

Usually, a process that overwrites its input is in danger of clobbering the input before it finishes reading it, unless it's specifically designed to be able to handle that case.

The simplest solution is to write to a separate output file, then when the transformation is finished, delete or move/rename the input file, and move/rename the output file to be what the input file used to be.

LarsH
  • 27,481
  • 8
  • 94
  • 152
  • Thanks for the reply. Well its showing the following exception and error when I use the same path- javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Premature end of file. Error: Premature end of file. – RahulD Aug 04 '12 at 04:12
  • Currently I'm doing the same as you said i.e. Move the file but according to my project's requirement doing it without moving would have been better. Rename the file sounds good. – RahulD Aug 04 '12 at 04:16
  • I deleted the original file and then renamed the newly generated file with the name of original file and it worked. Thanks for the suggestion. :) – RahulD Aug 04 '12 at 05:34
1

If Anyone else is facing the same problem then have a look what I have done as per LarsH's suggestion and it works perfectly-

public static void main(String[] args) {
    String inXML = "C:/tmp/text.xml";
    String inXSL = "C:/tmp/text.xsl";
    String outTXT = "C:/tmp/text_copy_copy.xml";
    String renamedFile = "C:/tmp/text.xml";
    File oldfile =new File(outTXT);
    File newfile =new File(renamedFile);
    SimpleXSLT st = new SimpleXSLT();
    try {
    //TRANSFORMATION CODE
    }

    try{     
        File file = new File(inXML);
        if(file.delete()){
            System.out.println("Deleted!");
        }else{
            System.out.println("Failed.");
        }

    }catch(Exception e){

        e.printStackTrace();

    }

    if(oldfile.renameTo(newfile)){
        System.out.println("Renamed");
    }else{
        System.out.println("Rename failed");
    }

    }
RahulD
  • 709
  • 2
  • 16
  • 39