1. First, import the package. Download the Axiom package from Apache.
2. Place the XSD and XML files in the bin path of the project.
Package xmlvalidate;
Import java. Io. bytearrayinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. ioexception;
Import javax. xml. xmlconstants;
Import javax. xml. Stream. factoryconfigurationerror;
Import javax. xml. Stream. xmlinputfactory;
Import javax. xml. Stream. xmlstreamexception;
Import javax. xml. Stream. xmlstreamreader;
Import javax. xml. Transform. source;
Import javax. xml. Transform. Stream. streamsource;
Import javax. xml. validation. Schema;
Import javax. xml. validation. schemafactory;
Import javax. xml. validation. validator;
Import org. Apache. Axiom. om. omelement;
Import org. Apache. Axiom. om. impl. Builder. staxombuilder;
Import org. xml. Sax. saxexception;
Public class schemavalidate {
/**
* Validate the XML file with XML schema file
*
* Place the XSD and XML files in the bin path of the project.
* @ Throws saxexception
* @ Throws ioexception
*/
Public static Boolean validate (string schemalocaltion, omelement request)
Throws saxexception, ioexception
{
// Obtain the schema factory class
// Here the xmlconstants. w3c_xml_schema_ns_uri value is: // http://www.w3.org/2001/XMLSchema
Schemafactory factory = schemafactory. newinstance (xmlconstants. w3c_xml_schema_ns_uri );
// Schema instance
Schema schema = NULL;
// Get the XSD file and read it to source as a stream
// The Position of the XSD file relative to the class file
Source schemasource = new streamsource (schemavalidate. Class. getresourceasstream (schemalocaltion ));
// Instantiate the schema object
Schema = factory. newschema (schemasource );
// Here, a DOM tree object is converted into a stream object to verify the DOM tree object.
// If You Want To verify the XML file, use fileinputstream.
String input = request. tostring ();
Bytearrayinputstream BAIS = new bytearrayinputstream (input. getbytes ("UTF-8 "));
// Obtain the validators. the XML schema source of the validators is the schema created earlier.
Validator = schema. newvalidator ();
Source source = new streamsource (BAIS );
// Perform verification
Try
{
Validator. Validate (source );
Return true;
}
Catch (exception ex)
{
Return false;
}
}
/**
* Get omelement SOAP request from specified XML file.
*
* @ Param request
* @ Return
* @ Throws filenotfoundexception
* @ Throws xmlstreamexception
* @ Throws factoryconfigurationerror
*/
Public static omelement getrequest (string filepath)
Throws filenotfoundexception, xmlstreamexception,
Factoryconfigurationerror {
Xmlstreamreader reader = xmlinputfactory. newinstance (). createxmlstreamreader (schemavalidate. Class. getresourceasstream (filepath ));
Staxombuilder builder = new staxombuilder (Reader );
Omelement requestmessage = builder. getdocumentelement ();
Return requestmessage;
}
Public static void main (string [] ARGs) // throws saxexception, ioexception,
// Xmlstreamexception, factoryconfigurationerror
{
Try
{
// If (validate ("/customer. XSD", getrequest ("/customer. xml ")))
//{
// System. Out. println ("customer. XML format is good ");
//} Else
//{
// System. Out. println ("the format of customer. XML is incorrect. Check the format! ");
//}
If (validate ("/customer. XSD", getrequest ("/customer_err.xml ")))
{
System. Out. println ("customer_err.xml format is good ");
} Else
{
System. Out. println ("customer_err.xml format error, please check! ");
}
}
Catch (exception ex)
{
System. Out. println ("the file format is incorrect. Please check it! ");
}
}
}