KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > io > xml > Dom4JWriter


1 package com.thoughtworks.xstream.io.xml;
2
3 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
4 import org.dom4j.Branch;
5 import org.dom4j.DocumentFactory;
6 import org.dom4j.Element;
7
8 import java.util.LinkedList JavaDoc;
9
10 public class Dom4JWriter implements HierarchicalStreamWriter {
11
12     private DocumentFactory documentFactory = new DocumentFactory();
13     private LinkedList JavaDoc elementStack = new LinkedList JavaDoc();
14
15     public Dom4JWriter(Branch container) {
16         elementStack.addLast(container);
17     }
18
19     public void startNode(String JavaDoc name) {
20         Element element = documentFactory.createElement(name);
21         top().add(element);
22         elementStack.addLast(element);
23     }
24
25     public void setValue(String JavaDoc text) {
26         top().setText(text);
27     }
28
29     public void addAttribute(String JavaDoc key, String JavaDoc value) {
30         ((Element) top()).addAttribute(key, value);
31     }
32
33     public void endNode() {
34         elementStack.removeLast();
35     }
36
37     private Branch top() {
38         return (Branch) elementStack.getLast();
39     }
40
41 }
42
Popular Tags