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

Tag.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.
XMLGenerator.java
- Sample program which utilize this Tag generator and creates XML for you.

Tag.java:


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() {
setTagName(UNDEFINED);
}

public Tag(String tagName) {
setTagName(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();
str.append("").append(NEWLINE).append(
generateTag());
return str;
}

public StringBuilder generateTag() {
StringBuilder str = new StringBuilder();
str.append(LT).append(getTagName());
if (!getAttrList().isEmpty()) {
str.append(generateAtribute(getAttrList()));
}
if (!getChildList().isEmpty()) {
str.append(GT);
for (Tag childTag : getChildList()) {
str.append(NEWLINE).append(childTag.generateTag());
}
str.append(NEWLINE).append(LT).append(BACK_SLASH).append(
getTagName()).append(GT);
} else {
str.append(WHITE_SPACE).append(BACK_SLASH).append(GT);
}
return str;
}

private StringBuilder generateAtribute(Map<String, String> attrMap) {
StringBuilder str = new StringBuilder();
for (Map.Entry<String, String> attribute : attrMap.entrySet()) {
str.append(WHITE_SPACE).append(attribute.getKey()).append(EQUAL)
.append(DOUBLE_QUOTE).append(attribute.getValue()).append(DOUBLE_QUOTE);
}

return str;
}
/* Business methods -end */
}

Here you go with testing

XMLGenerator.java:


package javax;

public class XMLGenerator {

public static void main(String[] args) {
Tag tag = new Tag("documentprotocol");
tag.getAttrList().put("docversion","1.0");
tag.getAttrList().put("docprotocol","custom");

Tag doctypetag=new Tag("DocumentType");
tag.getChildList().add(doctypetag);

System.out.println(tag.generatewithProcessTag());
}
}

No comments:

Post a Comment

Recent Posts

Unix Commands | List all My Posts

Texts

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