Return to Snippet

Revision: 3804
at September 14, 2007 18:10 by jsbournival


Initial Code
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;

import org.dom4j.io.*;
import org.dom4j.*;


public class MyClass {
	
    public static void main(String[] args) {

        File dir = new File("in/emissions");
        File[] files = dir.listFiles();
        for (int i=0; i<files.length; i++) {
            if (!files[i].getName().endsWith(".xml")) {
                continue;
            }

            XMLWriter output = null;
            try {
                Element emission = new SAXReader().read(files[i]).getRootElement();

                Element sondage = emission.addElement("pr:sondage");
                sondage.addAttribute("xsi:nil", "true");
                sondage.setText("n/a");

                FileWriter writer = new FileWriter("out/" + files[i].getName());
                output = new XMLWriter(writer, OutputFormat.createPrettyPrint());

                output.write(emission);		
            } catch (IOException e) {
                e.printStackTrace();
            } catch (DocumentException e)
                e.printStackTrace();
            } finally {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Initial URL

                                

Initial Description

                                

Initial Title
Modifying XML file structure (Java)

Initial Tags
java

Initial Language
Groovy