Create a handler to save the error information found during verification.
- Import java. util. arraylist;
- Import java. util. List;
- Import org. xml. Sax. errorhandler;
- Import org. xml. Sax. saxexception;
- Import org. xml. Sax. saxparseexception;
- /**
- * @ Author Haiyan Wang
- * @ Version 1 Oct 26,200 7
- */
- Public class xmlparsererrorhander implements errorhandler {
- Private list <string> errors;
- Private int Index = 1;
- /**
- * Return errors
- *
- */
- Public list <string> geterrors (){
- Return errors;
- }
- /**
- * @ Param errors
- * The errors to set
- */
- Public void seterrors (list <string> errors ){
- This. Errors = errors;
- }
- /**
- *
- */
- Public xmlparsererrorhander (){
- Errors = new arraylist <string> ();
- }
- /* (Non-javadoc)
- * @ See org. xml. Sax. errorhandler # error (Org. xml. Sax. saxparseexception)
- */
- Public void error (saxparseexception exception) throws saxexception {
- Errors. Add (exception. getmessage ());
- }
- /* (Non-javadoc)
- * @ See org. xml. Sax. errorhandler # fatalError (Org. xml. Sax. saxparseexception)
- */
- Public void fatalError (saxparseexception exception) throws saxexception {
- Errors. Add (exception. getmessage ());
- }
- /* (Non-javadoc)
- * @ See org. xml. Sax. errorhandler # warning (Org. xml. Sax. saxparseexception)
- */
- Public void warning (saxparseexception exception) throws saxexception {
- Errors. Add (exception. getmessage ());
- }
- Public void adderror (string error ){
- Errors. Add (error );
- }
- Public Boolean haserror (){
- Return (errors! = NULL) & (errors. Size ()> 0 );
- }
- }
Parse XML method-the function of this method is to parse the imported XML file into a dodument object and use schema to verify the XML during parsing. If XML is incorrect, the error will be saved in handler:
- /**
- * @ Param schemacontent
- * @ Param xmlcontent
- * @ Return
- * @ Throws exception
- */
- Public static document getdocmentnode (string xmlcontent, string schemacontent)
- Throws exception {
- If (logger. isdebugenabled ()){
- Logger. debug ("Enter getdocmentnode (string xmlcontent, string schemacontent )");
- }
- Inputstream is = new bytearrayinputstream (xmlcontent. getbytes ());
- Inputstream schemais = new bytearrayinputstream (schemacontent. getbytes ());
- Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
- Document Doc = NULL;
- Xmlparsererrorhander hander = new xmlparsererrorhander ();
- Try {
- Schemafactory = schemafactory. newinstance ("http://www.w3.org/2001/XMLSchema ");
- Source schemasource = new streamsource (schemais );
- Schema schema = schemafactory. newschema (schemasource );
- Javax. xml. validation. validator = schema. newvalidator ();
- Source source = new streamsource (is );
- Validator. seterrorhandler (hander );
- Validator. Validate (source );
- Factory. setignoringcomments (true );
- Factory. setignoringelementcontentwhitespace (true );
- // As I tested before, if uses validator, the is wocould be changed,
- // So I have to create a new inputstream.
- Bytearrayinputstream is2 =
- New bytearrayinputstream (xmlcontent. getbytes ());
- Documentbuilder builder = factory. newdocumentbuilder ();
- Doc = builder. parse (is2 );
- Doc. normalize ();
- } Catch (parserconfigurationexception e ){
- Hander. geterrors (). Add (E. getmessage ());
- Logger. Error (E. getmessage (), e );
- } Catch (filenotfoundexception e ){
- Hander. geterrors (). Add (E. getmessage ());
- Logger. Error (E. getmessage (), e );
- } Catch (saxexception e ){
- Hander. geterrors (). Add (E. getmessage ());
- Logger. Error (E. getmessage (), e );
- } Catch (ioexception e ){
- Hander. geterrors (). Add (E. getmessage ());
- Logger. Error (E. getmessage (), e );
- }
- If (hander. geterrors (). Size ()> 0 ){
- Logger. Error ("met" + hander. geterrors (). Size () + "when parsed the XML .");
- Stringbuffer buffer =
- New stringbuffer ("");
- For (string error: hander. geterrors ()){
- Buffer. append (error). append (constants. error_message_split );
- }
- Buffer. Delete (buffer. lastindexof (constants. error_message_split), buffer. Length ());
- Logger. Error ("error met when get the document from the XML:" + buffer. tostring ());
- Throw new exception (buffer. tostring ());
- }
- If (logger. isdebugenabled ()){
- Logger. debug ("Exit getdocmentnode (string xmlcontent, string schemacontent )");
- }
- Return Doc;
- }
For XML verification, see:
Http://blog.csdn.net/haydenwang8287/archive/2007/09/13/1784398.aspx