KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > samples > CreateXMLDemo


1 /*
2  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: CreateXMLDemo.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
8  */

9
10 package org.dom4j.samples;
11
12 import java.io.FileWriter JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.util.Properties JavaDoc;
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 /**
23  * A sample program to demonstrate creating some XML output using DOM4J. This
24  * sample generates an XML document representing the state of the current JVM
25  * displaying the current system properties.
26  *
27  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan </a>
28  * @version $Revision: 1.4 $
29  */

30 public class CreateXMLDemo extends AbstractDemo {
31
32     public static void main(String JavaDoc[] args) {
33         run(new CreateXMLDemo(), args);
34     }
35
36     public CreateXMLDemo() {
37     }
38
39     public void run(String JavaDoc[] args) throws Exception JavaDoc {
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 JavaDoc fileName = args[0];
48             println("Writing file: " + fileName);
49             FileWriter JavaDoc out = new FileWriter JavaDoc(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 JavaDoc {
57         Document document = DocumentHelper.createDocument();
58         Element root = document.addElement("system");
59
60         Properties JavaDoc properties = System.getProperties();
61         for (Enumeration JavaDoc elements = properties.propertyNames(); elements
62                 .hasMoreElements();) {
63             String JavaDoc name = (String JavaDoc) elements.nextElement();
64             String JavaDoc 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 /*
74  * Redistribution and use of this software and associated documentation
75  * ("Software"), with or without modification, are permitted provided that the
76  * following conditions are met:
77  *
78  * 1. Redistributions of source code must retain copyright statements and
79  * notices. Redistributions must also contain a copy of this document.
80  *
81  * 2. Redistributions in binary form must reproduce the above copyright notice,
82  * this list of conditions and the following disclaimer in the documentation
83  * and/or other materials provided with the distribution.
84  *
85  * 3. The name "DOM4J" must not be used to endorse or promote products derived
86  * from this Software without prior written permission of MetaStuff, Ltd. For
87  * written permission, please contact dom4j-info@metastuff.com.
88  *
89  * 4. Products derived from this Software may not be called "DOM4J" nor may
90  * "DOM4J" appear in their names without prior written permission of MetaStuff,
91  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
92  *
93  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
94  *
95  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
96  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
97  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
98  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
99  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
100  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
101  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
102  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
103  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
104  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
105  * POSSIBILITY OF SUCH DAMAGE.
106  *
107  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
108  *
109  * $Id: CreateXMLDemo.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
110  */

111
Popular Tags