Home > Education > GWT XML Indenter/Formatter

GWT XML Indenter/Formatter

December 31st, 2008 Leave a comment Go to comments

So while I was working on one of my outreach projects as a graduate student, I wanted to write a simple XML indenter to make my GWT generated xml more aesthetically appealing using stock GWT. The xml document is assumed to be as lean as possible (There are no empty #text nodes that are usually in xml because of the indentation.)

I should also mention that I’m posting this because I didn’t really see any stock simple GWT indenters after a quick google. The following is not meant to be a complete indenter, just something quick and simple to organize xml.

Here’s a basic indenter.

	public String formatXML(Node node,String tab_str)
	{
		String formatted="";
 
		if (node.getNodeType()==Node.ELEMENT_NODE)
		{
			String attributes="";
			for (int k=0;k < node.getAttributes().getLength();k++)
				attributes+=" "+node.getAttributes().item(k).getNodeName()+"=\""+node.getAttributes().item(k).getNodeValue()+"\"";
 
			formatted=tab_str+"<"+node.getNodeName()+attributes+">\n";
 
			for (int i=0;i< node.getChildNodes().getLength();i++)
			{
				formatted=formatted+formatXML(node.getChildNodes().item(i),tab_str+"    ");
			}
			formatted=formatted+tab_str+"</"+node.getNodeName()+">\n";
		}
		else
		{
			if (node.toString().trim().length()>0)
				formatted=tab_str+node.toString()+"\n";
		}
 
		return formatted;
	}
Categories: Education Tags: , , , ,
  • caustic

    Your code would not pass my review.

  • http://jonblog.wong.lan Jon

    @caustic
    It would be nice if you explained your comment, it’s somewhat unclear how I should make improvements.

  • http://www.improve.dk Mark S. Rasmussen

    As for code style I’d prefer something like this (semi-pseudo code):
    http://pastebin.com/f7f14bbbf

    I really dislike the inconsistent usage of curlies. Lack of spaces between concatenations and parameter separations. Lack of temporary variable / for each loop (dunno if that’s possible here, my Java knowledge is limited) in the attribute loop.

  • Auke

    Well for one you might want to use a StringBuilder.

  • http://jonblog.wong.lan Jon

    Thanks a lot for the comments.

    Just some comments and some questions.

    Lack of spaces between concatenations and parameter separations.

    Lack of temporary variable / for each loop (dunno if that’s possible here, my Java knowledge is limited) in the attribute loop.

    Some of these are partly due to the way my wordpress formatter is misbehaving. It didn’t like the “< " in my for loops. The others are simply that I was lazy. I just wanted to make the snippet available. It's not optimized at all. Just short and easy to understand.

    Temporary variables would certainly make it more efficient, but to me it makes the code a little harder to just skim through. The attribute loop is actually an unfortunate circumstance of java. In IE, it seems to work well if I just run getAttributes().toString(). However in Firefox, it lacks the necessary spaces between each of the attributes, and so I resorted to adding in an extra loop. In case anyone wants to fix the snippet and use temporary variables, you should probably reference the NodeMap of attributes, perhaps in addition to referencing to the attribute node.

    Well for one you might want to use a StringBuilder.

    I would be interesting in seeing if anyone has run any tests on String vs StringBuilder in GWT. In theory StringBuilder should be more efficient, but I’m not sure how it’s been implemented in javascript. Either way, it’s probably more memory efficient, which I should probably keep in mind because GWT can get somewhat heavy.