Modifying XML file structure (Java)


/ Published in: Groovy
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import java.io.FileWriter;
  2. import java.io.File;
  3. import java.io.IOException;
  4.  
  5. import org.dom4j.io.*;
  6. import org.dom4j.*;
  7.  
  8.  
  9. public class MyClass {
  10.  
  11. public static void main(String[] args) {
  12.  
  13. File dir = new File("in/emissions");
  14. File[] files = dir.listFiles();
  15. for (int i=0; i<files.length; i++) {
  16. if (!files[i].getName().endsWith(".xml")) {
  17. }
  18.  
  19. XMLWriter output = null;
  20. try {
  21. Element emission = new SAXReader().read(files[i]).getRootElement();
  22.  
  23. Element sondage = emission.addElement("pr:sondage");
  24. sondage.addAttribute("xsi:nil", "true");
  25. sondage.setText("n/a");
  26.  
  27. FileWriter writer = new FileWriter("out/" + files[i].getName());
  28. output = new XMLWriter(writer, OutputFormat.createPrettyPrint());
  29.  
  30. output.write(emission);
  31. e.printStackTrace();
  32. } catch (DocumentException e)
  33. e.printStackTrace();
  34. } finally {
  35. try {
  36. output.close();
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41. }
  42. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.