Use XSD to verify XML in Java

Source: Internet
Author: User

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! ");
}

}
}

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.