java - XML parsing with DOM parser -


in app trying parse xml file using dom parser. if parsing successful file moved success directory else error directory.next files deleted source directory

the problem is, when ever ill formed xml file (ex: xml document missing end tags) exception throws along following error message.

"the process cannot access file because being used process."

due file not deleted source directory.

public  class xmlloader extends thread { boolean success =false; public xmlloader(soapconnection con, string xmlpath) {      try {         system.out.println("laoding xml...");         file file = new file(xmlpath);         documentbuilderfactory factory = documentbuilderfactory.newinstance();         documentbuilder builder = factory.newdocumentbuilder();         document document = builder.parse(file);         string xmlstring = null;          domsource domsource = new domsource(document);         stringwriter writer = new stringwriter();         streamresult result = new streamresult(writer);         transformerfactory tf = transformerfactory.newinstance();         transformer transformer = tf.newtransformer();         transformer.transform(domsource, result);         xmlstring = writer.tostring();         inboundcasexmlresponse cresponse = con.loadxmlcase(xmlstring);         system.out.println("soap response == "+cresponse);         if(cresponse.gethaserrors()== false)         {             success = true;          }       } catch (exception e) {         system.out.println(e.getmessage());          }   } public boolean getstatus() {     return success; } 

}

i found simle way..

private static boolean loadxml(soapconnection con, string xmlpath) {     boolean success =false;     fileinputstream file=null;     try {         system.out.println("loading xml...");         documentbuilderfactory factory = documentbuilderfactory.newinstance();         documentbuilder builder = factory.newdocumentbuilder();         file = new fileinputstream(xmlpath);         document document = builder.parse(file);         system.out.println(document.haschildnodes());         string xmlstring = null;                     domsource domsource = new domsource(document);         stringwriter writer = new stringwriter();         streamresult result = new streamresult(writer);         transformerfactory tf = transformerfactory.newinstance();         transformer transformer = tf.newtransformer();         transformer.transform(domsource, result);         xmlstring = writer.tostring();         inboundcasexmlresponse cresponse = con.loadxmlcase(xmlstring);         system.out.println("soap response == "+cresponse);         if(cresponse.gethaserrors()== false)         {             success = true;          }         } catch (exception e) {         system.out.println(e.getmessage());         try{         file.close();         }         catch(exception ex)         {         e.printstacktrace();             }          }      return success; } 

Comments