Write schema verification program (dom, dom4j) in java)

Source: Internet
Author: User

Recently I used schema to verify the xml file, searched for the information, and sorted it below.

There are few materials in China. I mainly refer to two foreign articles: http://www.ibm.com/?works/xml/library/x-javaxmlvalidapi/index.htmland

Http://tutorials.jenkov.com/java-xml/dom-schema-validation.html.

Code:

Import java. io. file; <br/> import javax. xml. transform. stream. streamSource; // StreamSource <br/> import javax. xml. *; // XMLConstants <br/> import javax. xml. validation. *; // XMLConstants, validator <br/> public class shemaTest {</p> <p>/** <br/> * @ param args <br/> */<br/> public static void main (String [] args) {<br/> // TODO Auto-generated method stub <br/> Schema schema = null; <br/> try {</p> <p> String language = XMLConstants. w3C_XML_SCHEMA_NS_URI; <br/> SchemaFactory = SchemaFactory. newInstance (language); <br/> schema = factory. newSchema (new File ("Schema. xml "); // Schema. the extension of xml file name to your schema can be xsd <br/> Validator validator = schema. newValidator (); <br/> validator. validate (new StreamSource ("patterns. xml "); // patterns. change xml to the xml file name you want to verify <br/>} catch (Exception e) {<br/> // TODO: handle exception <br/> System. out. println (e. getMessage (); <br/>}</p> <p >}< br/>If you have any questions, leave a message below. This method is irrelevant to the dom parsing method.

Related Article

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.