Tuesday, December 11, 2007

Create String XML without XMLDocument

How to create XML Document without using the XML Document Object ?
How to create XML Document using JAVA API ?

is there any easy way which helps to create XML Document programmatically without relying on any of the XML Parser Implementation ?.

I have created XMLDocument based on xerces, but my clients are using Oracle XDK, what to do ?.


Yes. Some features availed by giving customized methods and classes in Parser implementation. But, the same feature may not be available in other implementation.

Best and sweet approaches is, don't implement your application based on particular XML Parser implementation and try to use always the packages comes from rt.jar(javax.xml.*; org.w3c.*;). Some highly required features may not be available in these packages, hence we will force to use implementation specific features from parser implementation.

Before making use of packages which is not follows JSR 5 suggestions, do double check is that same method will be available for other parser, how they are handling the same feature.

Sometime Feature will give big head ache for us.

Here is the code how to convert XML Document Object to String xml-to-string-object.html

An Element is combination of child Elements and attributes and tagName, hence
create a class with these three components.

private String tagName;
private Map attrList = new HashMap();
private List childList = new ArrayList();

To call and get XML content as output
Tag tag = new Tag("Employees");
tag.getAttrList().put("name","Alex");
tag.getAttrList().put("address","ch 8509 zurich");
Tag depttag=new Tag("Department");
depttag.getAttrList().put("name","Engineering");
tag.getChildList().add(depttag);

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

the output will be
<?xml version=\"1.0\"?>
<employees address="ch 8509 zurich" name="Alex">
<department name="Engineering">
</employees>

To generate attribute and values to add in Element tag
for (Map.Entry attribute : attrMap.entrySet()) {
str.append(WHITE_SPACE).append(attribute.getKey()).append(EQUAL) .append(DOUBLE_QUOTE).append(attribute.getValue()).append( DOUBLE_QUOTE); }

To create Element as String object
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);
}





Here i have explained almost all how to do implementation to get XML document without relying on any of the XML implementations. Here, you can add n number of element without worrying about the implementation of org.w3c.dom.Document object, i.e no XML Parser implementation is required to create XML document.

This implementation contains only one class with 128 line of code. This will improve performance of XML document creation(obviously less feature will do better), and eates less heap size.


Please feel free to ask implementation by sending mail or leaving your valuable comments.

Reactions:

0 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

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More