Re: Creating an XML file

Posted by Tomas Karlsson-2 on
URL: http://imagej.273.s1.nabble.com/Creating-an-XML-file-tp3689647p3689653.html

19 jan 2010 kl. 11.52 skrev Wilson R S (AT):

> I there, tanks for that.
> Is there an easier way for example outputting as a text file and then
> renaming and altering the file extension?


A XML file is just a text file so if you save it with the XML file-ending and have it corectly encoded it will be a XML-file

// Tomas

import java.io.*;
import ij.*;
import ij.io.*;
import ij.plugin.*;
import javax.xml.stream.*;


public class Fio_ implements PlugIn { //

        public void run(String arg){

                String fn,dn;
               
                // promt user for filename to save the file as
                SaveDialog s = new  SaveDialog("Save the xml file as ","bla",".xml");
                fn = s.getFileName();
                dn = s.getDirectory();
                fn = dn + fn; // Concatenate to get the full pathname
                // Write a Text file with XML statments - note that the quotes has to be handeled manualy
                try{
                        FileOutputStream fos = new FileOutputStream(fn);
                        try{
                                OutputStreamWriter out = new OutputStreamWriter(fos, "UTF-8");
                                try{
                                        out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
                                        out.write("\t<ZereneStackerBatchScript>\n");
                                        // And a loot more writes
                                        out.close(); // Write the buffer and close the file
                                }
                                catch(IOException e){
                                       
                                }
                               
                        }
                        catch(java.io.UnsupportedEncodingException e){
                                // should abort the rest of the code
                        }
                       
                }
                catch (java.io.FileNotFoundException e){
                        // should abort the rest of the code
                }


                // Alternativly the propper way of writing a XML file
                fn = fn + "_2";
               
                XMLOutputFactory xof =  XMLOutputFactory.newInstance();
        XMLStreamWriter xtw = null;
        try {
            try {
                xtw = xof.createXMLStreamWriter(new FileOutputStream(fn), "UTF-8");
                xtw.writeStartDocument("utf-8","1.0");
                xtw.writeCharacters("\n");
                xtw.writeStartElement("ZereneStackerBatchScript");
                xtw.writeCharacters("\n");
                                //...
                   
                xtw.writeEndElement(); // closing ZereneStackerBatchScript
                xtw.writeEndDocument();
                xtw.close();
            } catch (XMLStreamException ex) {

            }
        } catch (FileNotFoundException ex) {

        }

               
        }
       


}