GWT XML Indenter/Formatter
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; } |