We know that either MyBatis or spring uses an XML file as a configuration file, and the format of the configuration file is defined in The. DTD or. xsd file, how do you know if a user-defined file is correct when the tool parses the User's own defined XML file? We can not write in the XML file some of the framework does not know the tags, such as the spring XML file to write the following <user> tags, will undoubtedly error. So how does the framework verify that the labels we write are correct?
< User > < ID ></ID></user>
Since MyBatis uses Dom parsing, the DOM parsing API for the JDK is as Follows:
1 @Test2 public voidDocfactorytest ()throwsException {3Documentbuilderfactory factory =documentbuilderfactory.newinstance ();4Factory.setvalidating (true);5Factory.setnamespaceaware (false);6Factory.setignoringcomments (true);7Factory.setignoringelementcontentwhitespace (false);8Factory.setcoalescing (false);9Factory.setexpandentityreferences (true);TenDocumentbuilder Builder =Factory.newdocumentbuilder (); oneBuilder.setentityresolver (NewXmlmapperentityresolver ());
... (part of the code is omitted Here) -Document doc = Builder.parse (resources.getresourceasstream ("mybatis-config.xml")); toSystem.err.println (doc);
In line 11th, the Xmlmapperentityresolver object is used, and the object is defined as Follows:
1 public classXmlmapperentityresolverImplementsEntityresolver {2 3 Private Static Finalmap<string, string> Doctypemap =Newhashmap<string, string>();4 5 Private Static FinalString ibatis_config_public = "-//ibatis.apache.org//dtd CONFIG 3.0//en". toUpperCase (locale.english);6 Private Static FinalString Ibatis_config_system = "http://ibatis.apache.org/dtd/ibatis-3-config.dtd". toUpperCase (locale.english);7 8 Private Static FinalString ibatis_mapper_public = "-//ibatis.apache.org//dtd MAPPER 3.0//en". toUpperCase (locale.english);9 Private Static FinalString Ibatis_mapper_system = "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd". toUpperCase (locale.english);Ten one Private Static FinalString mybatis_config_public = "-//mybatis.org//dtd CONFIG 3.0//en". toUpperCase (locale.english); a Private Static FinalString Mybatis_config_system = "http://mybatis.org/dtd/mybatis-3-config.dtd". toUpperCase (locale.english); - - Private Static FinalString mybatis_mapper_public = "-//mybatis.org//dtd MAPPER 3.0//en". toUpperCase (locale.english); the Private Static FinalString Mybatis_mapper_system = "http://mybatis.org/dtd/mybatis-3-mapper.dtd". toUpperCase (locale.english); - - Private Static FinalString MYBATIS_CONFIG_DTD = "org/apache/ibatis/builder/xml/mybatis-3-config.dtd"; - Private Static FinalString MYBATIS_MAPPER_DTD = "org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd"; + - Static { + doctypemap.put (ibatis_config_system, mybatis_config_dtd); a doctypemap.put (ibatis_config_public, mybatis_config_dtd); at - doctypemap.put (ibatis_mapper_system, mybatis_mapper_dtd); - doctypemap.put (ibatis_mapper_public, mybatis_mapper_dtd); - - doctypemap.put (mybatis_config_system, mybatis_config_dtd); - doctypemap.put (mybatis_config_public, mybatis_config_dtd); in - doctypemap.put (mybatis_mapper_system, mybatis_mapper_dtd); to doctypemap.put (mybatis_mapper_public, mybatis_mapper_dtd); + } - the /* * * Converts a public DTD into a local one $ * Panax Notoginseng * @param publicid the public ID that's what's comes after ' public ' - * @param systemid the System ID that's what comes after the public ID. the * @return The InputSource for the DTD + * a * @throws org.xml.sax.SAXException If Anything Goes wrong the */ + @Override - publicInputSource resolveentity (string publicid, string systemid)throwssaxexception { $ $ if(publicid! =NULL) { -publicID =publicid.touppercase (locale.english); - } the if(systemid! =NULL) { -SystemID =systemid.touppercase (locale.english);Wuyi } the -InputSource Source =NULL; wu Try { -String Path =Doctypemap.get (publicid); aboutSource =Getinputsource (path, source); $ if(source = =NULL) { -Path =Doctypemap.get (systemid); -Source =Getinputsource (path, source); - } a}Catch(Exception E) { + Throw Newsaxexception (e.tostring ()); the } - returnsource; $ } the the Privateinputsource getinputsource (String path, inputsource source) { the if(path! =NULL) { the InputStream in; - Try { inin =Resources.getresourceasstream (path); theSource =NewInputSource (in); the}Catch(ioexception E) { about //ignore, null is OK the } the } the returnsource; + } - the}
As you can see, when the framework parses the XML file, it passes the publicID and SystemID at the beginning of the XML file to entityresolver, and the Entityresolver object is looking for The. DTD file from the Classpath ( Files are in the org/apache/ibatis/builder/xml/directory), and in spring there will be an. XSD file that works the same, and then validates with The. DTD file in Classpath. If you do not specify This. DTD file, the. DTD file is downloaded from the Internet and is not performing Well.
Validation of XML file correctness with tools such as Mybatis,spring