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.