Tuesday, October 14, 2008

XML Tag Generator

Java Developers sometimes forced/asked to create XML document using any of the available implementations may be from IBM, Oracle, BEA, or SUN reference implementations. Relying on particular implementation which will stop improving the performance by including unwanted classes also in our classpath.

If your requirement is just to create XML document not doing any manipulation below is the sample code which will helps you to created easily the XML document with fully formattedXML.

TWO Java files: - Tag.java, XMLGenerator.java

- which holds the details of how the architecture looks like and how the attribute and elements will be hold and generated the XML string object out of it.
- Sample program which utilize this Tag generator and creates XML for you.


package javax;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Tag implements Serializable {

private static final long serialVersionUID = -7676167530337603084L;

private String tagName;
private Map<String, String> attrList = new HashMap<String, String>();
private List<Tag> childList = new ArrayList<Tag>();

private static final String LT = "<";
private static final String GT = ">";
private static final String BACK_SLASH = "/";
private static final String WHITE_SPACE = " ";
private static final String NEWLINE = "\n";
private static final String EQUAL = "=";
private static final String DOUBLE_QUOTE = "\"";
private static final String FORWARD_SLASH = "\\";
private static final String UNDEFINED = "Undefined";

public Tag() {

public Tag(String tagName) {

* @return the tagName
public String getTagName() {
return tagName;

* @param tagName
* the tagName to set
public void setTagName(String tagName) {
this.tagName = tagName;

* @return the attrList
public Map<String, String> getAttrList() {
if (null == attrList)
attrList = new HashMap<String, String>();
return attrList;

* @param attrList
* the attrList to set
public void setAttrList(Map<String, String> attrList) {
this.attrList = attrList;

* @return the childList
public List<Tag> getChildList() {
if (null == childList)
childList = new ArrayList<Tag>();
return childList;

* @param childList
* the childList to set
public void setChildList(List<Tag> childList) {
this.childList = childList;

/* Business methods -start */
public StringBuilder generatewithProcessTag() {
StringBuilder str = new StringBuilder();
return str;

public StringBuilder generateTag() {
StringBuilder str = new StringBuilder();
if (!getAttrList().isEmpty()) {
if (!getChildList().isEmpty()) {
for (Tag childTag : getChildList()) {
} else {
return str;

private StringBuilder generateAtribute(Map<String, String> attrMap) {
StringBuilder str = new StringBuilder();
for (Map.Entry<String, String> attribute : attrMap.entrySet()) {

return str;
/* Business methods -end */

Here you go with testing


package javax;

public class XMLGenerator {

public static void main(String[] args) {
Tag tag = new Tag("documentprotocol");

Tag doctypetag=new Tag("DocumentType");


No comments:

Post a Comment

Recent Posts

Unix Commands | List all My Posts


This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com