1 9 10 package org.dom4j.samples; 11 12 import java.io.FileWriter ; 13 import java.util.Enumeration ; 14 import java.util.Properties ; 15 16 import org.dom4j.Document; 17 import org.dom4j.DocumentHelper; 18 import org.dom4j.Element; 19 import org.dom4j.io.OutputFormat; 20 import org.dom4j.io.XMLWriter; 21 22 30 public class CreateXMLDemo extends AbstractDemo { 31 32 public static void main(String [] args) { 33 run(new CreateXMLDemo(), args); 34 } 35 36 public CreateXMLDemo() { 37 } 38 39 public void run(String [] args) throws Exception { 40 Document document = createDocument(); 41 OutputFormat format = new OutputFormat(" ", true); 42 43 if (args.length < 1) { 44 XMLWriter writer = new XMLWriter(System.out, format); 45 writer.write(document); 46 } else { 47 String fileName = args[0]; 48 println("Writing file: " + fileName); 49 FileWriter out = new FileWriter (args[0]); 50 XMLWriter writer = new XMLWriter(out, format); 51 writer.write(document); 52 out.close(); 53 } 54 } 55 56 protected Document createDocument() throws Exception { 57 Document document = DocumentHelper.createDocument(); 58 Element root = document.addElement("system"); 59 60 Properties properties = System.getProperties(); 61 for (Enumeration elements = properties.propertyNames(); elements 62 .hasMoreElements();) { 63 String name = (String ) elements.nextElement(); 64 String value = properties.getProperty(name); 65 Element element = root.addElement("property"); 66 element.addAttribute("name", name); 67 element.addText(value); 68 } 69 return document; 70 } 71 } 72 73 111 | Popular Tags |