Adding an element beneath the root based on condition : 
   
 I'm reading an xml file using SAX, then I'm comparing the children of the xml file to validation rules that I got from my database in (validateByRules)  Now based on the validation, I'm adding to each chilld, a subchild: <DESC><DESC/> . Now What I want is that after iterating all the children of the document, if at least one validation is wrong, in any of the children. Then an element is added beneath the root of name: <error>. For that I created a counter in VALIDATEbYrULES. Bur from there I'm not positively sure on how to continue. Any help?



       public void execute() throws Exception {
        	
            try{
            	
            	Document xmlDoc = convertXmlFileToDocument("c:\\TEST.xml");
                
            	String xmlType = xmlDoc.getRootElement().getAttributeValue("type"); 
            	
            	
            	HashMap<String, HashMap<String, String>> hashValidationRules = getRulesByType(xmlType);
            	
            	List<Element> childElem = xmlDoc.getRootElement().getChildren();
         		Element updatedElem = null;
           
            			updatedElem = validateByRules(hashValidationRules, childElem.get(j));
            			
            			xmlDoc.getRootElement().getChildren().remove(j);
            			xmlDoc.getRootElement().getChildren().add(j, updatedElem);
            			
    			}
            	
            
        }
    
        
        private Element validateByRules(HashMap<String, HashMap<String, String>> hashValidationRules,	Element element) {
    
        	HashMap<String, String> rules = null;
        	String desc = "";
        	String value = "";
        	int counter = 0;
        	String fileName = null;
        	for ( String key : hashValidationRules.keySet() ) {
        		rules = hashValidationRules.get(key);
        		
        		if(rules.get("mandatory").equals("true") && (element.getChild(key)==null || element.getChild(key).getValue().equals(""))){
        			
        			counter ++;
        			desc = "Mandatory Element ("+key+") does not exist";
        		}
        		
        		value = element.getChild(key).getValue();
        		if(value.length()> Integer.parseInt(rules.get("size"))){
        			counter ++;
        			desc = "Field "+key+" exceeded max size ("+rules.get("size")+")";
        		}
        		
        		
        	}
        	element.addContent(new Element("DESC").setText(desc));
        	System.out.println(counter);
        	return element;
        	
    	}
        
      
    
        private Document convertXmlFileToDocument(String path){
        	
        	SAXBuilder sb=new SAXBuilder();
        	File xmlFile = new File(path);
        	Document doc = null;
        	try {
        		doc = sb.build(xmlFile);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
        	return doc;
        }
     

