Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2009
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. public String formatXML(Node node, String tab_str)
  2. {
  3.     String formatted = "";
  4.  
  5.     if (node.getNodeType() == Node.ELEMENT_NODE)
  6.     {
  7.         String attributes = "";
  8.        
  9.         var attributes = node.getAttributes();
  10.         for (int k=0; k<attributes.getLength(); k++)
  11.         {
  12.             var attr = attributes[k];
  13.             attributes += " " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\"";
  14.         }
  15.        
  16.         formatted = tab_str + "<" + node.getNodeName() + attributes + ">\n";
  17.  
  18.         for (int i=0; i<node.getChildNodes().getLength(); i++)
  19.             formatted = formatted + formatXML(node.getChildNodes().item(i), tab_str + "    ");
  20.            
  21.         formatted = formatted + tab_str + "</" + node.getNodeName() + ">\n";
  22.     }
  23.     else
  24.         if (node.toString().trim().length() > 0)
  25.             formatted = tab_str + node.toString() + "\n";
  26.  
  27.     return formatted;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement