Verify XML code using schema

Source: Internet
Author: User

Create a handler to save the error information found during verification.

  1. Import java. util. arraylist;
  2. Import java. util. List;
  3. Import org. xml. Sax. errorhandler;
  4. Import org. xml. Sax. saxexception;
  5. Import org. xml. Sax. saxparseexception;
  6. /**
  7. * @ Author Haiyan Wang
  8. * @ Version 1 Oct 26,200 7
  9. */
  10. Public class xmlparsererrorhander implements errorhandler {
  11. Private list <string> errors;
  12. Private int Index = 1;
  13. /**
  14. * Return errors
  15. *
  16. */
  17. Public list <string> geterrors (){
  18. Return errors;
  19. }
  20. /**
  21. * @ Param errors
  22. * The errors to set
  23. */
  24. Public void seterrors (list <string> errors ){
  25. This. Errors = errors;
  26. }
  27. /**
  28. *
  29. */
  30. Public xmlparsererrorhander (){
  31. Errors = new arraylist <string> ();
  32. }
  33. /* (Non-javadoc)
  34. * @ See org. xml. Sax. errorhandler # error (Org. xml. Sax. saxparseexception)
  35. */
  36. Public void error (saxparseexception exception) throws saxexception {
  37. Errors. Add (exception. getmessage ());
  38. }
  39. /* (Non-javadoc)
  40. * @ See org. xml. Sax. errorhandler # fatalError (Org. xml. Sax. saxparseexception)
  41. */
  42. Public void fatalError (saxparseexception exception) throws saxexception {
  43. Errors. Add (exception. getmessage ());
  44. }
  45. /* (Non-javadoc)
  46. * @ See org. xml. Sax. errorhandler # warning (Org. xml. Sax. saxparseexception)
  47. */
  48. Public void warning (saxparseexception exception) throws saxexception {
  49. Errors. Add (exception. getmessage ());
  50. }
  51. Public void adderror (string error ){
  52. Errors. Add (error );
  53. }
  54. Public Boolean haserror (){
  55. Return (errors! = NULL) & (errors. Size ()> 0 );
  56. }
  57. }

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:

  1. /**
  2. * @ Param schemacontent
  3. * @ Param xmlcontent
  4. * @ Return
  5. * @ Throws exception
  6. */
  7. Public static document getdocmentnode (string xmlcontent, string schemacontent)
  8. Throws exception {
  9. If (logger. isdebugenabled ()){
  10. Logger. debug ("Enter getdocmentnode (string xmlcontent, string schemacontent )");
  11. }
  12. Inputstream is = new bytearrayinputstream (xmlcontent. getbytes ());
  13. Inputstream schemais = new bytearrayinputstream (schemacontent. getbytes ());
  14. Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
  15. Document Doc = NULL;
  16. Xmlparsererrorhander hander = new xmlparsererrorhander ();
  17. Try {
  18. Schemafactory = schemafactory. newinstance ("http://www.w3.org/2001/XMLSchema ");
  19. Source schemasource = new streamsource (schemais );
  20. Schema schema = schemafactory. newschema (schemasource );
  21. Javax. xml. validation. validator = schema. newvalidator ();
  22. Source source = new streamsource (is );
  23. Validator. seterrorhandler (hander );
  24. Validator. Validate (source );
  25. Factory. setignoringcomments (true );
  26. Factory. setignoringelementcontentwhitespace (true );
  27. // As I tested before, if uses validator, the is wocould be changed,
  28. // So I have to create a new inputstream.
  29. Bytearrayinputstream is2 =
  30. New bytearrayinputstream (xmlcontent. getbytes ());
  31. Documentbuilder builder = factory. newdocumentbuilder ();
  32. Doc = builder. parse (is2 );
  33. Doc. normalize ();
  34. } Catch (parserconfigurationexception e ){
  35. Hander. geterrors (). Add (E. getmessage ());
  36. Logger. Error (E. getmessage (), e );
  37. } Catch (filenotfoundexception e ){
  38. Hander. geterrors (). Add (E. getmessage ());
  39. Logger. Error (E. getmessage (), e );
  40. } Catch (saxexception e ){
  41. Hander. geterrors (). Add (E. getmessage ());
  42. Logger. Error (E. getmessage (), e );
  43. } Catch (ioexception e ){
  44. Hander. geterrors (). Add (E. getmessage ());
  45. Logger. Error (E. getmessage (), e );
  46. }
  47. If (hander. geterrors (). Size ()> 0 ){
  48. Logger. Error ("met" + hander. geterrors (). Size () + "when parsed the XML .");
  49. Stringbuffer buffer =
  50. New stringbuffer ("");
  51. For (string error: hander. geterrors ()){
  52. Buffer. append (error). append (constants. error_message_split );
  53. }
  54. Buffer. Delete (buffer. lastindexof (constants. error_message_split), buffer. Length ());
  55. Logger. Error ("error met when get the document from the XML:" + buffer. tostring ());
  56. Throw new exception (buffer. tostring ());
  57. }
  58. If (logger. isdebugenabled ()){
  59. Logger. debug ("Exit getdocmentnode (string xmlcontent, string schemacontent )");
  60. }
  61. Return Doc;
  62. }

For XML verification, see:

Http://blog.csdn.net/haydenwang8287/archive/2007/09/13/1784398.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.