Simple XML Creator in Java package xml.impl;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;



public class XMLCreator {

private String parent;
private String child;
private Integer ctr = 0;
private HashMap&lt;Integer, String&gt; xml;
private String root;
private HashMap&lt;Integer, String&gt; roots;
private StringBuilder sB;

private HashMap&lt;Integer, String&gt; getRoot() {
    this.roots.put(1, "&lt;" + root + "&gt;");
    this.roots.put(2, "&lt;/" + root + "&gt;");
    return roots;
}

public void setRoot(String root) {
    this.root = root;
}

public XMLCreator() {
    if (this.xml == null) {
        this.xml = new HashMap&lt;Integer, String&gt;();
    }
    if (this.roots == null) {
        this.roots = new HashMap&lt;Integer, String&gt;();
    }
    if (this.sB == null) {
        this.sB = new StringBuilder();
    }
}

/*
 * private void startParentField(String parent) { this.parent = "&lt;" + parent
 * + "&gt;"; this.ctr++; addToXML(getParentField()); }
 * 
 * private void endParentField(String parent) { this.parent = "&lt;/" + parent
 * + "&gt;"; this.ctr++; addToXML(getParentField()); }
 * 
 * public void setParent(String parent) { startParentField(parent);
 * endParentField(parent); }
 */

public void addChildField(String child) {
    this.child = "&lt;" + child + "&gt;" + "&lt;/" + child + "&gt;";
    this.ctr++;
    addToXML(getChildField());

}

public void addChildField(String child, String value) {
    this.child = "&lt;" + child + "&gt;" + value + "&lt;/" + child + "&gt;";
    this.ctr++;
    addToXML(getChildField());
}

private String getParentField() {
    return parent;
}

private String getChildField() {
    return child;
}

private void addToXML(String element) {
    xml.put(ctr, element);
}

public void buildXMLPreview() {

    // Set&lt;Integer&gt; keys = xml.keySet();
    getRoot();
    System.out.println(roots.get(1));
    sB.append(roots.get(1));
    for (int x = 1; x &lt;= xml.size(); x++) {
        sB.append(xml.get(x));
    }
    sB.append(roots.get(2));
    System.out.println(sB.toString());
    sB.delete(0, sB.length());
}

public void buildXMLToFile() throws IOException {

    FileWriter writer = new FileWriter("try.xml");
    getRoot();
    writer.append(roots.get(1));
    for (int x = 1; x &lt;= xml.size(); x++) {
        writer.append(xml.get(x));
    }
    writer.append(roots.get(2));
    writer.flush();
    writer.close();
}

}


Sample Implementation:

package xml.impl;

import java.io.IOException;

public class XMLCreatorTester {
public static void main(String[] args) throws IOException {
    XMLCreator xmlCreator = new XMLCreator();
    xmlCreator.setRoot("Art");
    xmlCreator.addChildField("name", "Mona Lisa");
    xmlCreator.addChildField("artist", "Leonardo Da Vinci");
    xmlCreator.addChildField("adsa");
    xmlCreator.buildXMLPreview();
    xmlCreator.buildXMLToFile();
}
}


Output:

&lt;Art&gt;
&lt;name&gt;Mona Lisa&lt;/name&gt;
&lt;artist&gt;Leonardo Da Vinci&lt;/artist&gt;
&lt;adsa&gt;&lt;/adsa&gt;
&lt;/Art&gt;


As you can see as of now I can only create the root element and child elements(note that the parent element implementation is commented out).

Good enough? Suggestions? Violent reactions? Thanks. :)
What's this?

public XMLCreator() {
    if (this.xml == null) {
        this.xml = new HashMap&lt;Integer, String&gt;();
    }
    if (this.roots == null) {
        this.roots = new HashMap&lt;Integer, String&gt;();
    }
    if (this.sB == null) {
        this.sB = new StringBuilder();
    }
}


The null checks are always true, where should a value come from?

Why 

this.child = "&lt;" + child + "&gt;" + "&lt;/" + child + "&gt;";


and not 

this.child = "&lt;" + child + "/&gt;";


?