XML validation for multiple schemas 验证使用多个XSD schema的XML文件

很多情况下我们为了优化XSD文件的可读性和可维护性,以及复用等问题的时候我们需要将schema文件拆分成多个,本文将着重关注于使用多个schema文件验证单一XML文件的问题(注: XML validation for multiple schemas)

 

下面将通过以下几个步骤演示如何使用多个schema(XSD)文件验证单一XML文件

1. 创建需要被验证的XML文件

2. 根据XML反向创建XSD文件

3. 使用多个schema验证XML文件

4. 运行测试

 

现在将逐步展开演示:

1. 创建需要被验证的XML文件

Xml代码   收藏代码
  1. <? xml   version = "1.0"   encoding = "utf-8"   ?>   
  2. < employees   xmlns:admin = "http://www.company.com/management/employees/admin" >   
  3.     < admin:employee >   
  4.         < admin:userId > johnsmith@company.com </ admin:userId >   
  5.         < admin:password > abc123_ </ admin:password >   
  6.         < admin:name > John Smith </ admin:name >   
  7.         < admin:age > 24 </ admin:age >   
  8.         < admin:gender > Male </ admin:gender >   
  9.     </ admin:employee >   
  10.     < admin:employee >   
  11.         < admin:userId > christinechen@company.com </ admin:userId >   
  12.         < admin:password > 123456 </ admin:password >   
  13.         < admin:name > Christine Chen </ admin:name >   
  14.         < admin:age > 27 </ admin:age >   
  15.         < admin:gender > Female </ admin:gender >   
  16.     </ admin:employee >   
  17. </ employees >   

 

 

2. 根据XML反向创建XSD文件

注:本文是反向生成的XSD文件,当然您可能是已经有XSD文件,那就可以直接跳过第二步了。

 

通过观察employees.xml的格式我们可以反向的创建出employees.xsd文件,但是为了快捷起见,我们可以选择使用转换工具(XML to XSD)来完成这项工作,这里我将使用trang:http://www.thaiopensource.com/relaxng/trang.html

 

首先下载最新版的trang.jar文件,然后将employees.xml和trang.jar放在同一个目录下,运行如下命令行:

java -jar trang.jar employees.xml employees.xsd

运行之后将会在当前目录下生成两个XSD文件:employees.xsd, admin.xsd, 如下:

 

employees.xsd

Xml代码   收藏代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < xs:schema   xmlns:xs = "http://www.w3.org/2001/XMLSchema"   elementFormDefault = "qualified"   xmlns:admin = "http://www.company.com/management/employees/admin" >   
  3.   < xs:import   namespace = "http://www.company.com/management/employees/admin"   schemaLocation = "admin.xsd" />   
  4.   < xs:element   name = "employees" >   
  5.     < xs:complexType >   
  6.       < xs:sequence >   
  7.         < xs:element   maxOccurs = "unbounded"   ref = "admin:employee" />   
  8.       </ xs:sequence >   
  9.     </ xs:complexType >   
  10.   </ xs:element >   
  11. </ xs:schema >   

 

admin.xsd

Xml代码   收藏代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < xs:schema   xmlns:xs = "http://www.w3.org/2001/XMLSchema"   elementFormDefault = "qualified"   targetNamespace = "http://www.company.com/management/employees/admin"   xmlns:admin = "http://www.company.com/management/employees/admin" >   
  3.   < xs:import   schemaLocation = "employees.xsd" />   
  4.   < xs:element   name = "employee" >   
  5.     < xs:complexType >   
  6.       < xs:sequence >   
  7.         < xs:element   ref = "admin:userId" />   
  8.         < xs:element   ref = "admin:password" />   
  9.         < xs:element   ref = "admin:name" />   
  10.         < xs:element   ref = "admin:age" />   
  11.         < xs:element   ref = "admin:gender" />   
  12.       </ xs:sequence >   
  13.     </ xs:complexType >   
  14.   </ xs:element >   
  15.   < xs:element   name = "userId"   type = "xs:string" />   
  16.   < xs:element   name = "password"   type = "xs:NMTOKEN" />   
  17.   < xs:element   name = "name"   type = "xs:string" />   
  18.   < xs:element   name = "age"   type = "xs:integer" />   
  19.   < xs:element   name = "gender"   type = "xs:NCName" />   
  20. </ xs:schema >   

 

当然你也可以自己手动的去书写XSD文件。

 

 

3. 使用多个schema验证XML文件

 

如果想验证使用单一shema的XML,应该不会遇到太多问题,示例如下:

Java代码   收藏代码
  1. public   static   boolean  validateSingleSchema(File xml, File xsd) {  
  2.         boolean  legal =  false ;  
  3.           
  4.         try  {  
  5.             SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);  
  6.             Schema schema = sf.newSchema(xsd);  
  7.               
  8.             Validator validator = schema.newValidator();  
  9.             validator.validate(new  StreamSource(xml));  
  10.               
  11.             legal = true ;  
  12.         } catch  (Exception e) {  
  13.             legal = false ;  
  14.             log.error(e.getMessage());  
  15.         }  
  16.           
  17.         return  legal;  
  18.     }  
public static boolean validateSingleSchema(File xml, File xsd) {
		boolean legal = false;
		
		try {
			SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
			Schema schema = sf.newSchema(xsd);
			
			Validator validator = schema.newValidator();
			validator.validate(new StreamSource(xml));
			
			legal = true;
		} catch (Exception e) {
			legal = false;
        	log.error(e.getMessage());
		}
		
		return legal;
	}

 

 

但是当使用多个schema验证的时候会导致无法加载classpath外部的使用<xs:import>/<xs:include>加载的XSD文件,导致如下error message:

org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'admin:employee' to a(n) 'element declaration' component.

 

为了解决这个问题我们需要使用LSResourceResolver, SchemaFactory在解析shcema的时候可以使用LSResourceResolver加载外部资源。

代码如下:

Java代码   收藏代码
  1. package  com.javaeye.terrencexu.jaxb;  
  2.   
  3. import  java.io.File;  
  4. import  java.io.FileInputStream;  
  5. import  java.io.FileNotFoundException;  
  6. import  java.io.InputStream;  
  7. import  java.io.Reader;  
  8. import  java.net.URI;  
  9. import  java.net.URISyntaxException;  
  10.   
  11. import  org.apache.log4j.Logger;  
  12. import  org.w3c.dom.ls.LSInput;  
  13. import  org.w3c.dom.ls.LSResourceResolver;  
  14.   
  15. /**  
  16.  *   
  17.  * Implement LSResourceResolver to customize resource resolution when parsing schemas.  
  18.  * <p>  
  19.  * SchemaFactory uses a LSResourceResolver when it needs to locate external resources   
  20.  * while parsing schemas, although exactly what constitutes "locating external resources"   
  21.  * is up to each schema language.   
  22.  * </p>  
  23.  * <p>  
  24.  * For example, for W3C XML Schema, this includes files &lt;include&gt;d or &lt;import&gt;ed,   
  25.  * and DTD referenced from schema files, etc.  
  26.  *</p>  
  27.  *  
  28.  */   
  29. class  SchemaResourceResolver  implements  LSResourceResolver {  
  30.   
  31.     private   static   final  Logger log = Logger.getLogger(SchemaResourceResolver. class );  
  32.       
  33.     /**  
  34.      *   
  35.      * Allow the application to resolve external resources.   
  36.      *   
  37.      * <p>  
  38.      * The LSParser will call this method before opening any external resource, including   
  39.      * the external DTD subset, external entities referenced within the DTD, and external   
  40.      * entities referenced within the document element (however, the top-level document   
  41.      * entity is not passed to this method). The application may then request that the   
  42.      * LSParser resolve the external resource itself, that it use an alternative URI,   
  43.      * or that it use an entirely different input source.   
  44.      * </p>  
  45.      *   
  46.      * <p>  
  47.      * Application writers can use this method to redirect external system identifiers to   
  48.      * secure and/or local URI, to look up public identifiers in a catalogue, or to read   
  49.      * an entity from a database or other input source (including, for example, a dialog box).  
  50.      * </p>  
  51.      */   
  52.     public  LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {  
  53.         log.info("/n>> Resolving "  +  "/n"   
  54.                           + "TYPE: "  + type +  "/n"   
  55.                           + "NAMESPACE_URI: "  + namespaceURI +  "/n"    
  56.                           + "PUBLIC_ID: "  + publicId +  "/n"   
  57.                           + "SYSTEM_ID: "  + systemId +  "/n"   
  58.                           + "BASE_URI: "  + baseURI +  "/n" );  
  59.           
  60.         String schemaLocation = baseURI.substring(0 , baseURI.lastIndexOf( "/" ) +  1 );  
  61.           
  62.         if (systemId.indexOf( "http://" ) <  0 ) {  
  63.             systemId = schemaLocation + systemId;  
  64.         }  
  65.           
  66.         LSInput lsInput = new  LSInputImpl();  
  67.           
  68.         URI uri = null ;  
  69.         try  {  
  70.             uri = new  URI(systemId);  
  71.         } catch  (URISyntaxException e) {  
  72.             e.printStackTrace();  
  73.         }  
  74.           
  75.         File file = new  File(uri);  
  76.         FileInputStream is = null ;  
  77.         try  {  
  78.             is = new  FileInputStream(file);  
  79.         } catch  (FileNotFoundException e) {  
  80.             e.printStackTrace();  
  81.         }  
  82.           
  83.         lsInput.setSystemId(systemId);  
  84.         lsInput.setByteStream(is);  
  85.           
  86.         return  lsInput;  
  87.     }  
  88.       
  89.     /**  
  90.      *   
  91.      * Represents an input source for data  
  92.      *  
  93.      */   
  94.     class  LSInputImpl  implements  LSInput {  
  95.   
  96.         private  String publicId;  
  97.         private  String systemId;  
  98.         private  String baseURI;  
  99.         private  InputStream byteStream;  
  100.         private  Reader charStream;  
  101.         private  String stringData;  
  102.         private  String encoding;  
  103.         private   boolean  certifiedText;  
  104.           
  105.         public  LSInputImpl() {}  
  106.           
  107.         public  LSInputImpl(String publicId, String systemId, InputStream byteStream) {  
  108.             this .publicId = publicId;  
  109.             this .systemId = systemId;  
  110.             this .byteStream = byteStream;  
  111.         }  
  112.           
  113.         public  String getBaseURI() {  
  114.             return  baseURI;  
  115.         }  
  116.   
  117.         public  InputStream getByteStream() {  
  118.             return  byteStream;  
  119.         }  
  120.   
  121.         public   boolean  getCertifiedText() {  
  122.             return  certifiedText;  
  123.         }  
  124.   
  125.         public  Reader getCharacterStream() {  
  126.             return  charStream;  
  127.         }  
  128.   
  129.         public  String getEncoding() {  
  130.             return  encoding;  
  131.         }  
  132.   
  133.         public  String getPublicId() {  
  134.             return  publicId;  
  135.         }  
  136.   
  137.         public  String getStringData() {  
  138.             return  stringData;  
  139.         }  
  140.   
  141.         public  String getSystemId() {  
  142.             return  systemId;  
  143.         }  
  144.   
  145.         public   void  setBaseURI(String baseURI) {  
  146.             this .baseURI = baseURI;  
  147.         }  
  148.   
  149.         public   void  setByteStream(InputStream byteStream) {  
  150.             this .byteStream = byteStream;  
  151.         }  
  152.   
  153.         public   void  setCertifiedText( boolean  certifiedText) {  
  154.             this .certifiedText = certifiedText;  
  155.         }  
  156.   
  157.         public   void  setCharacterStream(Reader characterStream) {  
  158.             this .charStream = characterStream;  
  159.         }  
  160.   
  161.         public   void  setEncoding(String encoding) {  
  162.             this .encoding = encoding;  
  163.         }  
  164.   
  165.         public   void  setPublicId(String publicId) {  
  166.             this .publicId = publicId;  
  167.         }  
  168.   
  169.         public   void  setStringData(String stringData) {  
  170.             this .stringData = stringData;  
  171.         }  
  172.   
  173.         public   void  setSystemId(String systemId) {  
  174.             this .systemId = systemId;  
  175.         }  
  176.           
  177.     }  
  178.   
  179. }  
package com.javaeye.terrencexu.jaxb;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.log4j.Logger;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;

/**
 * 
 * Implement LSResourceResolver to customize resource resolution when parsing schemas.
 * <p>
 * SchemaFactory uses a LSResourceResolver when it needs to locate external resources 
 * while parsing schemas, although exactly what constitutes "locating external resources" 
 * is up to each schema language. 
 * </p>
 * <p>
 * For example, for W3C XML Schema, this includes files &lt;include&gt;d or &lt;import&gt;ed, 
 * and DTD referenced from schema files, etc.
 *</p>
 *
 */
class SchemaResourceResolver implements LSResourceResolver {

	private static final Logger log = Logger.getLogger(SchemaResourceResolver.class);
	
	/**
	 * 
	 * Allow the application to resolve external resources. 
	 * 
	 * <p>
	 * The LSParser will call this method before opening any external resource, including 
	 * the external DTD subset, external entities referenced within the DTD, and external 
	 * entities referenced within the document element (however, the top-level document 
	 * entity is not passed to this method). The application may then request that the 
	 * LSParser resolve the external resource itself, that it use an alternative URI, 
	 * or that it use an entirely different input source. 
	 * </p>
	 * 
	 * <p>
	 * Application writers can use this method to redirect external system identifiers to 
	 * secure and/or local URI, to look up public identifiers in a catalogue, or to read 
	 * an entity from a database or other input source (including, for example, a dialog box).
	 * </p>
	 */
	public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
		log.info("/n>> Resolving " + "/n"
				          + "TYPE: " + type + "/n"
				          + "NAMESPACE_URI: " + namespaceURI + "/n" 
				          + "PUBLIC_ID: " + publicId + "/n"
				          + "SYSTEM_ID: " + systemId + "/n"
				          + "BASE_URI: " + baseURI + "/n");
		
		String schemaLocation = baseURI.substring(0, baseURI.lastIndexOf("/") + 1);
		
		if(systemId.indexOf("http://") < 0) {
			systemId = schemaLocation + systemId;
		}
		
		LSInput lsInput = new LSInputImpl();
		
		URI uri = null;
		try {
			uri = new URI(systemId);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		
		File file = new File(uri);
		FileInputStream is = null;
		try {
			is = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		lsInput.setSystemId(systemId);
		lsInput.setByteStream(is);
		
		return lsInput;
	}
	
	/**
	 * 
	 * Represents an input source for data
	 *
	 */
	class LSInputImpl implements LSInput {

		private String publicId;
		private String systemId;
		private String baseURI;
		private InputStream byteStream;
		private Reader charStream;
		private String stringData;
		private String encoding;
		private boolean certifiedText;
		
		public LSInputImpl() {}
		
		public LSInputImpl(String publicId, String systemId, InputStream byteStream) {
			this.publicId = publicId;
			this.systemId = systemId;
			this.byteStream = byteStream;
		}
		
		public String getBaseURI() {
			return baseURI;
		}

		public InputStream getByteStream() {
			return byteStream;
		}

		public boolean getCertifiedText() {
			return certifiedText;
		}

		public Reader getCharacterStream() {
			return charStream;
		}

		public String getEncoding() {
			return encoding;
		}

		public String getPublicId() {
			return publicId;
		}

		public String getStringData() {
			return stringData;
		}

		public String getSystemId() {
			return systemId;
		}

		public void setBaseURI(String baseURI) {
			this.baseURI = baseURI;
		}

		public void setByteStream(InputStream byteStream) {
			this.byteStream = byteStream;
		}

		public void setCertifiedText(boolean certifiedText) {
			this.certifiedText = certifiedText;
		}

		public void setCharacterStream(Reader characterStream) {
			this.charStream = characterStream;
		}

		public void setEncoding(String encoding) {
			this.encoding = encoding;
		}

		public void setPublicId(String publicId) {
			this.publicId = publicId;
		}

		public void setStringData(String stringData) {
			this.stringData = stringData;
		}

		public void setSystemId(String systemId) {
			this.systemId = systemId;
		}
		
	}

}

 

 

最后要做的事情就是创建一个validator去封装XML验证的逻辑代码, 如下:

Java代码   收藏代码
  1. package  com.javaeye.terrencexu.jaxb;  
  2.   
  3. import  java.io.File;  
  4. import  java.io.IOException;  
  5. import  java.io.InputStream;  
  6. import  java.io.StringWriter;  
  7. import  java.util.List;  
  8.   
  9. import  javax.xml.parsers.DocumentBuilder;  
  10. import  javax.xml.parsers.DocumentBuilderFactory;  
  11. import  javax.xml.parsers.ParserConfigurationException;  
  12. import  javax.xml.transform.Source;  
  13. import  javax.xml.transform.dom.DOMSource;  
  14. import  javax.xml.transform.stream.StreamSource;  
  15. import  javax.xml.validation.Schema;  
  16. import  javax.xml.validation.SchemaFactory;  
  17. import  javax.xml.validation.Validator;  
  18.   
  19. import  org.apache.log4j.Logger;  
  20. import  org.xml.sax.SAXException;  
  21.   
  22. public   final   class  XMLParser {  
  23.   
  24.     private   static   final  Logger log = Logger.getLogger(XMLParser. class );  
  25.       
  26.     private  XMLParser() {}  
  27.       
  28.     public   static   boolean  validateWithSingleSchema(File xml, File xsd) {  
  29.         boolean  legal =  false ;  
  30.           
  31.         try  {  
  32.             SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);  
  33.             Schema schema = sf.newSchema(xsd);  
  34.               
  35.             Validator validator = schema.newValidator();  
  36.             validator.validate(new  StreamSource(xml));  
  37.               
  38.             legal = true ;  
  39.         } catch  (Exception e) {  
  40.             legal = false ;  
  41.             log.error(e.getMessage());  
  42.         }  
  43.           
  44.         return  legal;  
  45.     }  
  46.       
  47.     public   static   boolean  validateWithMultiSchemas(InputStream xml, List<File> schemas) {  
  48.         boolean  legal =  false ;  
  49.           
  50.         try  {  
  51.             Schema schema = createSchema(schemas);  
  52.               
  53.             Validator validator = schema.newValidator();  
  54.             validator.validate(new  StreamSource(xml));  
  55.               
  56.             legal = true ;  
  57.         } catch (Exception e) {  
  58.             legal = false ;  
  59.             log.error(e.getMessage());  
  60.         }  
  61.           
  62.         return  legal;  
  63.     }  
  64.       
  65.     /**  
  66.      * Create Schema object from the schemas file.  
  67.      *   
  68.      * @param schemas  
  69.      * @return  
  70.      * @throws ParserConfigurationException  
  71.      * @throws SAXException  
  72.      * @throws IOException  
  73.      */   
  74.     private   static  Schema createSchema(List<File> schemas)  throws  ParserConfigurationException, SAXException, IOException {  
  75.         SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);  
  76.         SchemaResourceResolver resourceResolver = new  SchemaResourceResolver();  
  77.         sf.setResourceResolver(resourceResolver);  
  78.           
  79.         Source[] sources = new  Source[schemas.size()];  
  80.           
  81.         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();  
  82.         docFactory.setValidating(false );  
  83.         docFactory.setNamespaceAware(true );  
  84.         DocumentBuilder docBuilder = docFactory.newDocumentBuilder();  
  85.           
  86.         for ( int  i =  0 ; i < schemas.size(); i ++) {  
  87.             org.w3c.dom.Document doc = docBuilder.parse(schemas.get(i));  
  88.             DOMSource stream = new  DOMSource(doc, schemas.get(i).getAbsolutePath());  
  89.             sources[i] = stream;  
  90.         }  
  91.           
  92.         return  sf.newSchema(sources);  
  93.     }  
  94.       
  95. }  
package com.javaeye.terrencexu.jaxb;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;

public final class XMLParser {

	private static final Logger log = Logger.getLogger(XMLParser.class);
	
	private XMLParser() {}
	
	public static boolean validateWithSingleSchema(File xml, File xsd) {
		boolean legal = false;
		
		try {
			SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
			Schema schema = sf.newSchema(xsd);
			
			Validator validator = schema.newValidator();
			validator.validate(new StreamSource(xml));
			
			legal = true;
		} catch (Exception e) {
			legal = false;
        	log.error(e.getMessage());
		}
		
		return legal;
	}
	
	public static boolean validateWithMultiSchemas(InputStream xml, List<File> schemas) {
        boolean legal = false;
		
		try {
            Schema schema = createSchema(schemas);
            
            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(xml));
            
            legal = true;
        } catch(Exception e) {
        	legal = false;
        	log.error(e.getMessage());
        }
		
		return legal;
	}
	
	/**
	 * Create Schema object from the schemas file.
	 * 
	 * @param schemas
	 * @return
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 * @throws IOException
	 */
	private static Schema createSchema(List<File> schemas) throws ParserConfigurationException, SAXException, IOException {
		SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		SchemaResourceResolver resourceResolver = new SchemaResourceResolver();
		sf.setResourceResolver(resourceResolver);
		
		Source[] sources = new Source[schemas.size()];
		
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		docFactory.setValidating(false);
		docFactory.setNamespaceAware(true);
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
		
		for(int i = 0; i < schemas.size(); i ++) {
			org.w3c.dom.Document doc = docBuilder.parse(schemas.get(i));
			DOMSource stream = new DOMSource(doc, schemas.get(i).getAbsolutePath());
			sources[i] = stream;
		}
		
		return sf.newSchema(sources);
	}
	
}

 

4. 运行测试

 

Java代码   收藏代码
  1. public   static   void  testValidate()  throws  SAXException, FileNotFoundException {  
  2.         InputStream xml = new  FileInputStream( new  File( "C://eclipse//workspace1//JavaStudy//test//employees.xml" ));  
  3.           
  4.         List<File> schemas = new  ArrayList<File>();  
  5.         schemas.add(new  File( "C://eclipse//workspace1//JavaStudy//test//employees.xsd" ));  
  6.         schemas.add(new  File( "C://eclipse//workspace1//JavaStudy//test//admin.xsd" ));  
  7.           
  8.         XMLParser.validateWithMultiSchemas(xml, schemas);  
  9.     }  
public static void testValidate() throws SAXException, FileNotFoundException {
		InputStream xml = new FileInputStream(new File("C://eclipse//workspace1//JavaStudy//test//employees.xml"));
		
		List<File> schemas = new ArrayList<File>();
		schemas.add(new File("C://eclipse//workspace1//JavaStudy//test//employees.xsd"));
		schemas.add(new File("C://eclipse//workspace1//JavaStudy//test//admin.xsd"));
		
		XMLParser.validateWithMultiSchemas(xml, schemas);
	}

 

注:如果两个schema文件在同一个目录下,那么可以只传递一个主schema文件(employees.xsd)即可, SchemaResourceResolver会帮我们加载admin.xsd

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值