- import java.io.FileWriter;
- import java.io.IOException;
-
- import org.dom4j.Document;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- import org.dom4j.io.OutputFormat;
- import org.dom4j.io.XMLWriter;
-
- public class DWriter {
-
- public static void main(String[] args) {
-
- try {
- XMLWriter writer = new XMLWriter(new FileWriter("src/author.xml"));
- Document doc = createDoc();
- writer.write(doc);
- writer.close();
-
-
-
- OutputFormat format = OutputFormat.createPrettyPrint();
- writer = new XMLWriter(System.out, format);
- writer.write(doc);
-
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
-
- public static Document createDoc() {
- Document doc = DocumentHelper.createDocument();
- Element root = doc.addElement("root");
- Element author1 = root.addElement("author").addAttribute("name",
- "Kree").addAttribute("location", "UK")
- .addText("Kree Strachan");
- Element author2 = root.addElement("author").addAttribute("name", "King")
- .addAttribute("location", "US").addText("King McWrirter");
- return doc;
- }
- }