2
\$\begingroup\$
 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<Integer, String> xml;
private String root;
private HashMap<Integer, String> roots;
private StringBuilder sB;
private HashMap<Integer, String> getRoot() {
 this.roots.put(1, "<" + root + ">");
 this.roots.put(2, "</" + root + ">");
 return roots;
}
public void setRoot(String root) {
 this.root = root;
}
public XMLCreator() {
 if (this.xml == null) {
 this.xml = new HashMap<Integer, String>();
 }
 if (this.roots == null) {
 this.roots = new HashMap<Integer, String>();
 }
 if (this.sB == null) {
 this.sB = new StringBuilder();
 }
}
/*
 * private void startParentField(String parent) { this.parent = "<" + parent
 * + ">"; this.ctr++; addToXML(getParentField()); }
 * 
 * private void endParentField(String parent) { this.parent = "</" + parent
 * + ">"; this.ctr++; addToXML(getParentField()); }
 * 
 * public void setParent(String parent) { startParentField(parent);
 * endParentField(parent); }
 */
public void addChildField(String child) {
 this.child = "<" + child + ">" + "</" + child + ">";
 this.ctr++;
 addToXML(getChildField());
}
public void addChildField(String child, String value) {
 this.child = "<" + child + ">" + value + "</" + child + ">";
 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<Integer> keys = xml.keySet();
 getRoot();
 System.out.println(roots.get(1));
 sB.append(roots.get(1));
 for (int x = 1; x <= 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 <= 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:

<Art>
<name>Mona Lisa</name>
<artist>Leonardo Da Vinci</artist>
<adsa></adsa>
</Art>

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. :)

asked Sep 13, 2011 at 2:50
\$\endgroup\$
1
  • \$\begingroup\$ how does your code work for nested to more than just a single level ? \$\endgroup\$ Commented Oct 24, 2013 at 6:15

1 Answer 1

1
\$\begingroup\$

What's this?

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

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

Why

this.child = "<" + child + ">" + "</" + child + ">";

and not

this.child = "<" + child + "/>";

?

answered Sep 13, 2011 at 6:43
\$\endgroup\$
2
  • \$\begingroup\$ As you can see in my sample implementation when XMLCreator is instanciated it checks if those values are null... But yeah I get your point I guess when I instanciate XMLCreator those values are null so no need to check. :) Anyway with the child yeah when I view the xml generated on IE it shows that if I use that statement it would give me only '</child>', what gives? If an xml element is empty the closing tag is the one only shown? \$\endgroup\$ Commented Sep 13, 2011 at 8:18
  • \$\begingroup\$ According to the XML standard, <bla></bla> is equivalent to <bla/> (note that the "/" is at the end). \$\endgroup\$ Commented Sep 15, 2011 at 7:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.