精华内容
下载资源
问答
  • 使用xsd文件验证xml

    万次阅读 2016-01-19 17:50:34
    项目中遇到了需要对xml数据进行入库的需求,但是在入库之前需要利用xsd文件xml的合法性进行验证。 在网上找代码,找到了 leizhimin 的BLOG ,将其整理。感谢博主分享代码。 代码: import java.io.File; ...

    项目中遇到了需要对xml数据进行入库的需求,但是在入库之前需要利用xsd文件对xml的合法性进行验证。

    在网上找代码,找到了 leizhimin 的BLOG ,将其整理。感谢博主分享代码。


    代码:

    import java.io.File;
    
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    
    import org.dom4j.Document;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.SAXValidator;
    import org.dom4j.io.XMLWriter;
    import org.dom4j.util.XMLErrorHandler;
    import org.junit.Test;
    
    
    
    public class XmlValidator {
    	String XMLPath = "E:\\tmp\\spring.xml";
    	String xsdPath = "E:\\tmp\\valid-spring.xsd";
    	@Test
    	public void parser() throws Exception {
    		validateXMLByXSD(XMLPath,xsdPath);
    	}
    	/** 
         * 通过XSD(XML Schema)校验XML 
         */ 
        public static void validateXMLByXSD(String xmlFileName,String xsdFileName) { 
            try { 
                //创建默认的XML错误处理器 
                XMLErrorHandler errorHandler = new XMLErrorHandler(); 
                //获取基于 SAX 的解析器的实例 
                SAXParserFactory factory = SAXParserFactory.newInstance(); 
                //解析器在解析时验证 XML 内容。 
                factory.setValidating(true); 
                //指定由此代码生成的解析器将提供对 XML 名称空间的支持。 
                factory.setNamespaceAware(true); 
                //使用当前配置的工厂参数创建 SAXParser 的一个新实例。 
                SAXParser parser = factory.newSAXParser(); 
                //创建一个读取工具 
                SAXReader xmlReader = new SAXReader(); 
                //获取要校验xml文档实例 
                Document xmlDocument = (Document) xmlReader.read(new File(xmlFileName)); 
                //设置 XMLReader 的基础实现中的特定属性。核心功能和属性列表可以在 [url]http://sax.sourceforge.net/?selected=get-set[/url] 中找到。 
                parser.setProperty( 
                        "http://java.sun.com/xml/jaxp/properties/schemaLanguage", 
                        "http://www.w3.org/2001/XMLSchema"); 
                parser.setProperty( 
                        "http://java.sun.com/xml/jaxp/properties/schemaSource", 
                        "file:" + xsdFileName); 
                //创建一个SAXValidator校验工具,并设置校验工具的属性 
                SAXValidator validator = new SAXValidator(parser.getXMLReader()); 
                //设置校验工具的错误处理器,当发生错误时,可以从处理器对象中得到错误信息。 
                validator.setErrorHandler(errorHandler); 
                //校验 
                validator.validate(xmlDocument); 
    
                XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint()); 
                //如果错误信息不为空,说明校验失败,打印错误信息 
                if (errorHandler.getErrors().hasContent()) { 
                    System.out.println("XML文件通过XSD文件校验失败!"); 
                    writer.write(errorHandler.getErrors()); 
                } else { 
                    System.out.println("Good! XML文件通过XSD文件校验成功!"); 
                } 
            } catch (Exception ex) { 
                System.out.println("XML文件: " + xmlFileName + " 通过XSD文件:" + xsdFileName + "检验失败。\n原因: " + ex.getMessage()); 
                ex.printStackTrace(); 
            } 
        } 
    }
    


    spring.xml: 使用spring的配置文件进行验证:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    	
    	
    	<bean
    		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
    	
    	<bean id="viewResolver"
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/" />
    		<property name="suffix" value=".jsp" />
    		<property name="viewClass"
    			value="org.springframework.web.servlet.view.JstlView" />
    	</bean>
    	
    	 
        <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
        </bean>
    </beans>


    valid-spring.xml : 其实就是http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 下载下来改的名称。

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    
    <xsd:schema xmlns="http://www.springframework.org/schema/beans"
    		xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    		targetNamespace="http://www.springframework.org/schema/beans">
    
    	<xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
    
    	<xsd:annotation>
    		<xsd:documentation><![CDATA[
    	Spring XML Beans Schema, version 3.0
    	Authors: Juergen Hoeller, Rob Harrop, Mark Fisher
    
    	This defines a simple and consistent way of creating a namespace
    	of JavaBeans objects, managed by a Spring BeanFactory, read by
    	XmlBeanDefinitionReader (with DefaultBeanDefinitionDocumentReader).
    
    	This document type is used by most Spring functionality, including
    	web application contexts, which are based on bean factories.
    
    	Each "bean" element in this document defines a JavaBean.
    	Typically the bean class is specified, along with JavaBean properties
    	and/or constructor arguments.
    
    	A bean instance can be a "singleton" (shared instance) or a "prototype"
    	(independent instance). Further scopes can be provided by extended
    	bean factories, for example in a web environment.
    
    	References among beans are supported, that is, setting a JavaBean property
    	or a constructor argument to refer to another bean in the same factory
    	(or an ancestor factory).
    
    	As alternative to bean references, "inner bean definitions" can be used.
    	Singleton flags of such inner bean definitions are effectively ignored:
    	inner beans are typically anonymous prototypes.
    
    	There is also support for lists, sets, maps, and java.util.Properties
    	as bean property types or constructor argument types.
    		]]></xsd:documentation>
    	</xsd:annotation>
    
    	<!-- base types -->
    	<xsd:complexType name="identifiedType" abstract="true">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	The unique identifier for a bean. The scope of the identifier
    	is the enclosing bean factory.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:attribute name="id" type="xsd:ID">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	The unique identifier for a bean.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    	</xsd:complexType>
    
    	<!-- Top-level <beans> tag -->
    	<xsd:element name="beans">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	The top level (typically root) element. Allows the definition
    	of default values for all nested bean definitions.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:sequence>
    				<xsd:element ref="description" minOccurs="0"/>
    				<xsd:choice minOccurs="0" maxOccurs="unbounded">
    					<xsd:element ref="import"/>
    					<xsd:element ref="alias"/>
    					<xsd:element ref="bean"/>
    					<xsd:any namespace="##other" processContents="strict" minOccurs="0" maxOccurs="unbounded"/>
    				</xsd:choice>
    			</xsd:sequence>
    			<xsd:attribute name="default-lazy-init" default="false" type="xsd:boolean">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	The default 'lazy-init' value; see the documentation for the
    	'lazy-init' attribute of the '<bean>' element.
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    			<xsd:attribute name="default-merge" default="false" type="xsd:boolean">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	The default 'merge' value; see the documentation for the
    	'merge' attribute of the various collection elements.
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    			<xsd:attribute name="default-autowire" default="no">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	The default 'autowire' value; see the documentation for the
    	'autowire' attribute of the '<bean>' element.
    					]]></xsd:documentation>
    				</xsd:annotation>
    				<xsd:simpleType>
    					<xsd:restriction base="xsd:NMTOKEN">
    						<xsd:enumeration value="no"/>
    						<xsd:enumeration value="byName"/>
    						<xsd:enumeration value="byType"/>
    						<xsd:enumeration value="constructor"/>
    					</xsd:restriction>
    				</xsd:simpleType>
    			</xsd:attribute>
    			<xsd:attribute name="default-autowire-candidates" type="xsd:string">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	A default bean name pattern for identifying autowire candidates:
    	e.g. "*Service", "data*", "*Service*", "data*Service".
    	Also accepts a comma-separated list of patterns: e.g. "*Service,*Dao".
    	See the documentation for the 'autowire-candidate' attribute of the
    	'<bean/>' element for the semantic details of autowire candidate beans.
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    			<xsd:attribute name="default-init-method" type="xsd:string">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	The default 'init-method' value; see the documentation for the
    	'init-method' attribute of the '<bean>' element.
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    			<xsd:attribute name="default-destroy-method" type="xsd:string">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	The default 'destroy-method' value; see the documentation for the
    	'destroy-method' attribute of the '<bean>' element.
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    			<xsd:anyAttribute namespace="##other" processContents="lax"/>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="description">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	Contains informative text describing the purpose of the enclosing element.
    	Used primarily for user documentation of XML bean definition documents.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType mixed="true">
    			<xsd:choice minOccurs="0" maxOccurs="unbounded"/>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="import">
    		<xsd:annotation>
    			<xsd:documentation source="java:org.springframework.core.io.Resource"><![CDATA[
    	Specifies an XML bean definition resource to import.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:complexContent>
    				<xsd:restriction base="xsd:anyType">
    					<xsd:attribute name="resource" type="xsd:string" use="required">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	The relative resource location of the XML (bean definition) file to import,
    	for example "myImport.xml" or "includes/myImport.xml" or "../myImport.xml".
    							]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    				</xsd:restriction>
    			</xsd:complexContent>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="alias">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	Defines an alias for a bean (which can reside in a different definition
    	resource).
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:complexContent>
    				<xsd:restriction base="xsd:anyType">
    					<xsd:attribute name="name" type="xsd:string" use="required">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	The name of the bean to define an alias for.
    							]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    					<xsd:attribute name="alias" type="xsd:string" use="required">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	The alias name to define for the bean.
    							]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    				</xsd:restriction>
    			</xsd:complexContent>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:group name="beanElements">
    		<xsd:sequence>
    			<xsd:element ref="description" minOccurs="0"/>
    			<xsd:choice minOccurs="0" maxOccurs="unbounded">
    				<xsd:element ref="meta"/>
    				<xsd:element ref="constructor-arg"/>
    				<xsd:element ref="property"/>
    				<xsd:element ref="qualifier"/>
    				<xsd:element ref="lookup-method"/>
    				<xsd:element ref="replaced-method"/>
    				<xsd:any namespace="##other" processContents="strict" minOccurs="0" maxOccurs="unbounded"/>
    			</xsd:choice>
    		</xsd:sequence>
    	</xsd:group>
    
    	<xsd:attributeGroup name="beanAttributes">
    		<xsd:attribute name="name" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	Can be used to create one or more aliases illegal in an (XML) id.
    	Multiple aliases can be separated by any number of spaces, commas,
    	or semi-colons (or indeed any mixture of the three).
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="class" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation source="java:java.lang.Class"><![CDATA[
    	The fully qualified name of the bean's class, except if it serves only
    	as a parent definition for child bean definitions.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="parent" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	The name of the parent bean definition.
    
    	Will use the bean class of the parent if none is specified, but can
    	also override it. In the latter case, the child bean class must be
    	compatible with the parent, i.e. accept the parent's property values
    	and constructor argument values, if any.
    
    	A child bean definition will inherit constructor argument values,
    	property values and method overrides from the parent, with the option
    	to add new values. If init method, destroy method, factory bean and/or
    	factory method are specified, they will override the corresponding
    	parent settings.
    
    	The remaining settings will always be taken from the child definition:
    	depends on, autowire mode, scope, lazy init.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="scope" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	The scope of this bean: typically "singleton" (one shared instance,
    	which will be returned by all calls to getBean with the given id), or
    	"prototype" (independent instance resulting from each call to getBean).
    
    	By default, a bean will be a singleton, unless the bean has a parent
    	bean definition in which case it will inherit the parent's scope.
    
    	Singletons are most commonly used, and are ideal for multi-threaded
    	service objects. Further scopes, such as "request" or "session", might
    	be supported by extended bean factories (e.g. in a web environment).
    
    	Inner bean definitions inherit the singleton status of their containing
    	bean definition, unless explicitly specified: The inner bean will be a
    	singleton if the containing bean is a singleton, and a prototype if
    	the containing bean has any other scope.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="abstract" type="xsd:boolean">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	Is this bean "abstract", that is, not meant to be instantiated itself
    	but rather just serving as parent for concrete child bean definitions?
    	The default is "false". Specify "true" to tell the bean factory to not
    	try to instantiate that particular bean in any case.
    
    	Note: This attribute will not be inherited by child bean definitions.
    	Hence, it needs to be specified per abstract bean definition.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="lazy-init" default="default" type="defaultable-boolean">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	Indicates whether or not this bean is to be lazily initialized.
    	If false, it will be instantiated on startup by bean factories
    	that perform eager initialization of singletons. The default is
    	"false".
    
    	Note: This attribute will not be inherited by child bean definitions.
    	Hence, it needs to be specified per concrete bean definition.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="autowire" default="default">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	Controls whether bean properties are "autowired".
    	This is an automagical process in which bean references don't need
    	to be coded explicitly in the XML bean definition file, but rather the
    	Spring container works out dependencies.
    
    	There are 4 modes:
    
    	1. "no"
    	The traditional Spring default. No automagical wiring. Bean references
    	must be defined in the XML file via the <ref/> element (or "ref"
    	attribute). We recommend this in most cases as it makes documentation
    	more explicit.
    
    	Note that this default mode also allows for annotation-driven autowiring,
    	if activated. "no" refers to externally driven autowiring only, not
    	affecting any autowiring demands that the bean class itself expresses.
    
    	2. "byName"
    	Autowiring by property name. If a bean of class Cat exposes a "dog"
    	property, Spring will try to set this to the value of the bean "dog"
    	in the current container. If there is no matching bean by name, nothing
    	special happens.
    
    	3. "byType"
    	Autowiring if there is exactly one bean of the property type in the
    	container. If there is more than one, a fatal error is raised, and
    	you cannot use byType autowiring for that bean. If there is none,
    	nothing special happens.
    
    	4. "constructor"
    	Analogous to "byType" for constructor arguments. If there is not exactly
    	one bean of the constructor argument type in the bean factory, a fatal
    	error is raised.
    
    	Note that explicit dependencies, i.e. "property" and "constructor-arg"
    	elements, always override autowiring.
    
    	Note: This attribute will not be inherited by child bean definitions.
    	Hence, it needs to be specified per concrete bean definition.
    				]]></xsd:documentation>
    			</xsd:annotation>
    			<xsd:simpleType>
    				<xsd:restriction base="xsd:NMTOKEN">
    					<xsd:enumeration value="default"/>
    					<xsd:enumeration value="no"/>
    					<xsd:enumeration value="byName"/>
    					<xsd:enumeration value="byType"/>
    					<xsd:enumeration value="constructor"/>
    				</xsd:restriction>
    			</xsd:simpleType>
    		</xsd:attribute>
    		<xsd:attribute name="depends-on" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	The names of the beans that this bean depends on being initialized.
    	The bean factory will guarantee that these beans get initialized
    	before this bean.
    
    	Note that dependencies are normally expressed through bean properties
    	or constructor arguments. This property should just be necessary for
    	other kinds of dependencies like statics (*ugh*) or database preparation
    	on startup.
    
    	Note: This attribute will not be inherited by child bean definitions.
    	Hence, it needs to be specified per concrete bean definition.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="autowire-candidate" default="default" type="defaultable-boolean">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	Indicates whether or not this bean should be considered when looking
    	for matching candidates to satisfy another bean's autowiring requirements.
    	Note that this does not affect explicit references by name, which will get
    	resolved even if the specified bean is not marked as an autowire candidate.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="primary" type="xsd:boolean">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	Specifies that this bean should be given preference when multiple
    	candidates are qualified to autowire a single-valued dependency.
    	If exactly one 'primary' bean exists among the candidates, it
    	will be the autowired value.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="init-method" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	The name of the custom initialization method to invoke after setting
    	bean properties. The method must have no arguments, but may throw any
    	exception.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="destroy-method" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	The name of the custom destroy method to invoke on bean factory
    	shutdown. The method must have no arguments, but may throw any
    	exception.
    
    	Note: Only invoked on beans whose lifecycle is under the full
    	control of the factory - which is always the case for singletons,
    	but not guaranteed for any other scope.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="factory-method" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	The name of a factory method to use to create this object. Use
    	constructor-arg elements to specify arguments to the factory method,
    	if it takes arguments. Autowiring does not apply to factory methods.
    
    	If the "class" attribute is present, the factory method will be a static
    	method on the class specified by the "class" attribute on this bean
    	definition. Often this will be the same class as that of the constructed
    	object - for example, when the factory method is used as an alternative
    	to a constructor. However, it may be on a different class. In that case,
    	the created object will *not* be of the class specified in the "class"
    	attribute. This is analogous to FactoryBean behavior.
    
    	If the "factory-bean" attribute is present, the "class" attribute is not
    	used, and the factory method will be an instance method on the object
    	returned from a getBean call with the specified bean name. The factory
    	bean may be defined as a singleton or a prototype.
    
    	The factory method can have any number of arguments. Autowiring is not
    	supported. Use indexed constructor-arg elements in conjunction with the
    	factory-method attribute.
    
    	Setter Injection can be used in conjunction with a factory method.
    	Method Injection cannot, as the factory method returns an instance,
    	which will be used when the container creates the bean.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="factory-bean" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	Alternative to class attribute for factory-method usage.
    	If this is specified, no class attribute should be used.
    	This must be set to the name of a bean in the current or
    	ancestor factories that contains the relevant factory method.
    	This allows the factory itself to be configured using Dependency
    	Injection, and an instance (rather than static) method to be used.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:anyAttribute namespace="##other" processContents="lax"/>
    	</xsd:attributeGroup>
    
    	<xsd:element name="meta" type="metaType">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	Arbitrary metadata attached to a bean definition.
    			]]></xsd:documentation>
    		</xsd:annotation>
    	</xsd:element>
    
    	<xsd:complexType name="metaType">
    		<xsd:attribute name="key" type="xsd:string" use="required">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	The key name of the metadata attribute being defined.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="value" type="xsd:string" use="required">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	The value of the metadata attribute being defined (as a simple String).
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    	</xsd:complexType>
    
    	<xsd:element name="bean">
    		<xsd:annotation>
    			<xsd:documentation source="java:org.springframework.beans.factory.config.BeanDefinition"><![CDATA[
    	Defines a single (usually named) bean.
    
    	A bean definition may contain nested tags for constructor arguments,
    	property values, lookup methods, and replaced methods. Mixing constructor
    	injection and setter injection on the same bean is explicitly supported.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:complexContent>
    				<xsd:extension base="identifiedType">
    					<xsd:group ref="beanElements"/>
    					<xsd:attributeGroup ref="beanAttributes"/>
    				</xsd:extension>
    			</xsd:complexContent>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="constructor-arg">
    		<xsd:annotation>
    			<xsd:documentation source="java:org.springframework.beans.factory.config.ConstructorArgumentValues">
    				<![CDATA[
    	Bean definitions can specify zero or more constructor arguments.
    	This is an alternative to "autowire constructor".
    	Arguments correspond to either a specific index of the constructor
    	argument list or are supposed to be matched generically by type.
    
    	Note: A single generic argument value will just be used once, rather
    	than potentially matched multiple times (as of Spring 1.1).
    
    	constructor-arg elements are also used in conjunction with the
    	factory-method element to construct beans using static or instance
    	factory methods.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:sequence>
    				<xsd:element ref="description" minOccurs="0"/>
    				<xsd:choice minOccurs="0" maxOccurs="1">
    					<xsd:element ref="bean"/>
    					<xsd:element ref="ref"/>
    					<xsd:element ref="idref"/>
    					<xsd:element ref="value"/>
    					<xsd:element ref="null"/>
    					<xsd:element ref="array"/>
    					<xsd:element ref="list"/>
    					<xsd:element ref="set"/>
    					<xsd:element ref="map"/>
    					<xsd:element ref="props"/>
    					<xsd:any namespace="##other" processContents="strict"/>
    				</xsd:choice>
    			</xsd:sequence>
    			<xsd:attribute name="index" type="xsd:string">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	The exact index of the argument in the constructor argument list.
    	Only needed to avoid ambiguities, e.g. in case of 2 arguments of
    	the exact same type.
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    			<xsd:attribute name="type" type="xsd:string">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	The exact type of the constructor argument. Only needed to avoid
    	ambiguities, e.g. in case of 2 single argument constructors
    	that can both be converted from a String.
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    			<xsd:attribute name="name" type="xsd:string">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	The exact name of the argument in the constructor argument list.
    	Only needed to avoid ambiguities, e.g. in case of 2 arguments of
    	the exact same type. Note: This requires debug symbols to be
    	stored in the class file in order to introspect argument names!
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    			<xsd:attribute name="ref" type="xsd:string">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	A short-cut alternative to a nested "<ref bean='...'/>" element.
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    			<xsd:attribute name="value" type="xsd:string">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	A short-cut alternative to a nested "<value>...<value/>" element.
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="property" type="propertyType">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	Bean definitions can have zero or more properties.
    	Property elements correspond to JavaBean setter methods exposed
    	by the bean classes. Spring supports primitives, references to other
    	beans in the same or related factories, lists, maps and properties.
    			]]></xsd:documentation>
    		</xsd:annotation>
    	</xsd:element>
    
    	<xsd:element name="qualifier">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	Bean definitions can provide qualifiers to match against annotations
    	on a field or parameter for fine-grained autowire candidate resolution.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:sequence>
    				<xsd:element ref="attribute" minOccurs="0" maxOccurs="unbounded"/>
    			</xsd:sequence>
    			<xsd:attribute name="type" type="xsd:string" default="org.springframework.beans.factory.annotation.Qualifier"/>
    			<xsd:attribute name="value" type="xsd:string"/>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="attribute" type="metaType">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	A qualifier element may contain attribute child elements as key-value
    	pairs. These will be available for matching against attributes of a
    	qualifier annotation on an autowired field or parameter if present.
    			]]></xsd:documentation>
    		</xsd:annotation>
    	</xsd:element>
    
    	<xsd:element name="lookup-method">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	A lookup method causes the IoC container to override the given method
    	and return the bean with the name given in the bean attribute. This is
    	a form of Method Injection. It is particularly useful as an alternative
    	to implementing the BeanFactoryAware interface, in order to be able to
    	make getBean() calls for non-singleton instances at runtime. In this
    	case, Method Injection is a less invasive alternative.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:complexContent>
    				<xsd:restriction base="xsd:anyType">
    					<xsd:attribute name="name" type="xsd:string">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	The name of the lookup method. This method must take no arguments.
    							]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    					<xsd:attribute name="bean" type="xsd:string">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	The name of the bean in the current or ancestor factories that
    	the lookup method should resolve to. Often this bean will be a
    	prototype, in which case the lookup method will return a distinct
    	instance on every invocation. This is useful for single-threaded objects.
    							]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    				</xsd:restriction>
    			</xsd:complexContent>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="replaced-method">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	Similar to the lookup method mechanism, the replaced-method element
    	is used to control IoC container method overriding: Method Injection.
    	This mechanism allows the overriding of a method with arbitrary code.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:sequence>
    				<xsd:choice minOccurs="0" maxOccurs="unbounded">
    					<xsd:element ref="arg-type"/>
    				</xsd:choice>
    			</xsd:sequence>
    			<xsd:attribute name="name" type="xsd:string">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	The name of the method whose implementation must be replaced by the
    	IoC container. If this method is not overloaded, there is no need
    	to use arg-type subelements. If this method is overloaded, arg-type
    	subelements must be used for all override definitions for the method.
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    			<xsd:attribute name="replacer" type="xsd:string">
    				<xsd:annotation>
    					<xsd:documentation source="java:org.springframework.beans.factory.support.MethodReplacer"><![CDATA[
    	Bean name of an implementation of the MethodReplacer interface in the
    	current or ancestor factories. This may be a singleton or prototype
    	bean. If it is a prototype, a new instance will be used for each
    	method replacement. Singleton usage is the norm.
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="arg-type">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	Identifies an argument for a replaced method in the event of
    	method overloading.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType mixed="true">
    			<xsd:choice minOccurs="0" maxOccurs="unbounded"/>
    			<xsd:attribute name="match" type="xsd:string">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	Specification of the type of an overloaded method argument as a String.
    	For convenience, this may be a substring of the FQN. E.g. all the
    	following would match "java.lang.String":
    	- java.lang.String
    	- String
    	- Str
    
    	As the number of arguments will be checked also, this convenience
    	can often be used to save typing.
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="ref">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	Defines a reference to another bean in this factory or an external
    	factory (parent or included factory).
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:complexContent>
    				<xsd:restriction base="xsd:anyType">
    					<xsd:attribute name="bean" type="xsd:string">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	The name of the referenced bean.
    							]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    					<xsd:attribute name="local" type="xsd:IDREF">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	The name of the referenced bean. The value must be a bean ID and thus can 
    	be checked by the XML parser. This is therefore the preferred technique 
    	for referencing beans within the same bean factory XML file.
    							]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    					<xsd:attribute name="parent" type="xsd:string">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	The name of the referenced bean in a parent factory.
    						]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    				</xsd:restriction>
    			</xsd:complexContent>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="idref">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	The id of another bean in this factory or an external factory
    	(parent or included factory).
    	While a regular 'value' element could instead be used for the
    	same effect, using idref in this case allows validation of local
    	bean ids by the XML parser, and name completion by supporting tools.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:complexContent>
    				<xsd:restriction base="xsd:anyType">
    					<xsd:attribute name="bean" type="xsd:string">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	The name of the referenced bean.
    							]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    					<xsd:attribute name="local" type="xsd:IDREF">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	The name of the referenced bean. The value must be a bean ID and thus can 
    	be checked by the XML parser. This is therefore the preferred technique 
    	for referencing beans within the same bean factory XML file.
    							]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    				</xsd:restriction>
    			</xsd:complexContent>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="value">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	Contains a string representation of a property value.
    	The property may be a string, or may be converted to the required
    	type using the JavaBeans PropertyEditor machinery. This makes it
    	possible for application developers to write custom PropertyEditor
    	implementations that can convert strings to arbitrary target objects.
    
    	Note that this is recommended for simple objects only. Configure
    	more complex objects by populating JavaBean properties with
    	references to other beans.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType mixed="true">
    			<xsd:choice minOccurs="0" maxOccurs="unbounded"/>
    			<xsd:attribute name="type" type="xsd:string">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	The exact type that the value should be converted to. Only needed
    	if the type of the target property or constructor argument is
    	too generic: for example, in case of a collection element.
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="null">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	Denotes a Java null value. Necessary because an empty "value" tag
    	will resolve to an empty String, which will not be resolved to a
    	null value unless a special PropertyEditor does so.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType mixed="true">
    			<xsd:choice minOccurs="0" maxOccurs="unbounded"/>
    		</xsd:complexType>
    	</xsd:element>
    
    	<!-- Collection Elements -->
    	<xsd:group name="collectionElements">
    		<xsd:sequence>
    			<xsd:element ref="description" minOccurs="0"/>
    			<xsd:choice minOccurs="0" maxOccurs="unbounded">
    				<xsd:element ref="bean"/>
    				<xsd:element ref="ref"/>
    				<xsd:element ref="idref"/>
    				<xsd:element ref="value"/>
    				<xsd:element ref="null"/>
    				<xsd:element ref="array"/>
    				<xsd:element ref="list"/>
    				<xsd:element ref="set"/>
    				<xsd:element ref="map"/>
    				<xsd:element ref="props"/>
    				<xsd:any namespace="##other" processContents="strict" minOccurs="0" maxOccurs="unbounded"/>
    			</xsd:choice>
    		</xsd:sequence>
    	</xsd:group>
    
    	<xsd:element name="array">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	An array can contain multiple inner bean, ref, collection, or value elements.
    	This configuration element will always result in an array, even when being
    	defined e.g. as a value for a map with value type Object.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:complexContent>
    				<xsd:extension base="listOrSetType">
    					<xsd:attribute name="merge" default="default" type="defaultable-boolean">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	Enables/disables merging for collections when using parent/child beans.
    							]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    				</xsd:extension>
    			</xsd:complexContent>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="list">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	A list can contain multiple inner bean, ref, collection, or value elements.
    	A list can also map to an array type; the necessary conversion is performed
    	automatically.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:complexContent>
    				<xsd:extension base="listOrSetType">
    					<xsd:attribute name="merge" default="default" type="defaultable-boolean">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	Enables/disables merging for collections when using parent/child beans.
    							]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    				</xsd:extension>
    			</xsd:complexContent>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="set">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	A set can contain multiple inner bean, ref, collection, or value elements.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:complexContent>
    				<xsd:extension base="listOrSetType">
    					<xsd:attribute name="merge" default="default" type="defaultable-boolean">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	Enables/disables merging for collections when using parent/child beans.
    							]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    				</xsd:extension>
    			</xsd:complexContent>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="map">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	A mapping from a key to an object. Maps may be empty.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:complexContent>
    				<xsd:extension base="mapType">
    					<xsd:attribute name="merge" default="default" type="defaultable-boolean">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	Enables/disables merging for collections when using parent/child beans.
    							]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    				</xsd:extension>
    			</xsd:complexContent>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="entry" type="entryType">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	A map entry can be an inner bean, ref, value, or collection.
    	The key of the entry is given by the "key" attribute or child element.
    			]]></xsd:documentation>
    		</xsd:annotation>
    	</xsd:element>
    
    	<xsd:element name="props">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	Props elements differ from map elements in that values must be strings.
    	Props may be empty.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:complexContent>
    				<xsd:extension base="propsType">
    					<xsd:attribute name="merge" default="default" type="defaultable-boolean">
    						<xsd:annotation>
    							<xsd:documentation><![CDATA[
    	Enables/disables merging for collections when using parent/child beans.
    							]]></xsd:documentation>
    						</xsd:annotation>
    					</xsd:attribute>
    				</xsd:extension>
    			</xsd:complexContent>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="key">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	A key element can contain an inner bean, ref, value, or collection.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType>
    			<xsd:group ref="collectionElements"/>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:element name="prop">
    		<xsd:annotation>
    			<xsd:documentation><![CDATA[
    	The string value of the property. Note that whitespace is trimmed
    	off to avoid unwanted whitespace caused by typical XML formatting.
    			]]></xsd:documentation>
    		</xsd:annotation>
    		<xsd:complexType mixed="true">
    			<xsd:choice minOccurs="0" maxOccurs="unbounded"/>
    			<xsd:attribute name="key" type="xsd:string" use="required">
    				<xsd:annotation>
    					<xsd:documentation><![CDATA[
    	The key of the property entry.
    					]]></xsd:documentation>
    				</xsd:annotation>
    			</xsd:attribute>
    		</xsd:complexType>
    	</xsd:element>
    
    	<xsd:complexType name="propertyType">
    		<xsd:sequence>
    			<xsd:element ref="description" minOccurs="0"/>
    			<xsd:choice minOccurs="0" maxOccurs="1">
    				<xsd:element ref="meta"/>
    				<xsd:element ref="bean"/>
    				<xsd:element ref="ref"/>
    				<xsd:element ref="idref"/>
    				<xsd:element ref="value"/>
    				<xsd:element ref="null"/>
    				<xsd:element ref="array"/>
    				<xsd:element ref="list"/>
    				<xsd:element ref="set"/>
    				<xsd:element ref="map"/>
    				<xsd:element ref="props"/>
    				<xsd:any namespace="##other" processContents="strict"/>
    			</xsd:choice>
    		</xsd:sequence>
    		<xsd:attribute name="name" type="xsd:string" use="required">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	The name of the property, following JavaBean naming conventions.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="ref" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	A short-cut alternative to a nested "<ref bean='...'/>".
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="value" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	A short-cut alternative to a nested "<value>...</value>" element.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    	</xsd:complexType>
    
    	<!-- Collection Types -->
    
    	<!-- base type for collections that have (possibly) typed nested values -->
    	<xsd:complexType name="collectionType">
    		<xsd:attribute name="value-type" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation source="java:java.lang.Class"><![CDATA[
    	The default Java type for nested values. Must be a fully qualified
    	class name.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    	</xsd:complexType>
    
    	<!-- 'list' and 'set' collection type -->
    	<xsd:complexType name="listOrSetType">
    		<xsd:complexContent>
    			<xsd:extension base="collectionType">
    				<xsd:group ref="collectionElements"/>
    			</xsd:extension>
    		</xsd:complexContent>
    	</xsd:complexType>
    
    	<!-- 'map' element type -->
    	<xsd:complexType name="mapType">
    		<xsd:complexContent>
    			<xsd:extension base="collectionType">
    				<xsd:sequence>
    					<xsd:element ref="description" minOccurs="0"/>
    					<xsd:choice minOccurs="0" maxOccurs="unbounded">
    						<xsd:element ref="entry"/>
    					</xsd:choice>
    				</xsd:sequence>
    				<xsd:attribute name="key-type" type="xsd:string">
    					<xsd:annotation>
    						<xsd:documentation source="java:java.lang.Class"><![CDATA[
    	The default Java type for nested entry keys. Must be a fully qualified
    	class name.
    						]]></xsd:documentation>
    					</xsd:annotation>
    				</xsd:attribute>
    			</xsd:extension>
    		</xsd:complexContent>
    	</xsd:complexType>
    
    	<!-- 'entry' element type -->
    	<xsd:complexType name="entryType">
    		<xsd:sequence>
    			<xsd:element ref="key" minOccurs="0"/>
    			<xsd:group ref="collectionElements"/>
    		</xsd:sequence>
    		<xsd:attribute name="key" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	Each map element must specify its key as attribute or as child element.
    	A key attribute is always a String value.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="key-ref" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	A short-cut alternative to a to a "key" element with a nested
    	"<ref bean='...'/>".
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="value" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	A short-cut alternative to a nested "<value>...</value>"
    	element.
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    		<xsd:attribute name="value-ref" type="xsd:string">
    			<xsd:annotation>
    				<xsd:documentation><![CDATA[
    	A short-cut alternative to a nested "<ref bean='...'/>".
    				]]></xsd:documentation>
    			</xsd:annotation>
    		</xsd:attribute>
    	</xsd:complexType>
    
    	<!-- 'props' collection type -->
    	<xsd:complexType name="propsType">
    		<xsd:complexContent>
    			<xsd:extension base="collectionType">
    				<xsd:sequence>
    					<xsd:choice minOccurs="0" maxOccurs="unbounded">
    						<xsd:element ref="prop"/>
    					</xsd:choice>
    				</xsd:sequence>
    			</xsd:extension>
    		</xsd:complexContent>
    	</xsd:complexType>
    
    	<!-- simple internal types -->
    	<xsd:simpleType name="defaultable-boolean">
    		<xsd:restriction base="xsd:NMTOKEN">
    			<xsd:enumeration value="default"/>
    			<xsd:enumeration value="true"/>
    			<xsd:enumeration value="false"/>
    		</xsd:restriction>
    	</xsd:simpleType>
    
    </xsd:schema>
    


    运行结果:

    Good! XML文件通过XSD文件校验成功!

    展开全文
  • 主要介绍了C#使用xsd文件验证XML格式是否正确的实现方法,结合实例形式分析了C#针对xml文件的创建、验证相关操作技巧,需要的朋友可以参考下
  • C# 使用xsd文件验证XML 格式是否正确     核心示例代码: //创建xmlDocument XmlDocument doc = new XmlDocument(); //创建声明段 如&lt;?xml version="1.0" encoding="utf-8" ...

    C# 使用xsd文件验证XML 格式是否正确

     

     

    核心示例代码:

     //创建xmlDocument
                XmlDocument doc = new XmlDocument();
    
    //创建声明段 如<?xml version="1.0" encoding="utf-8" ?> 
                    doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
                    //创建一个根节点 KYTResults
                    XmlElement results = doc.CreateElement("KYTResults");
    
                    //创建 ResultsStatus
                    XmlNode resultsStatus = doc.CreateElement("ResultsStatus");
                    //创建Level
                    XmlElement element = doc.CreateElement("Level");
                    element.InnerText = status ? "0" : "1";
                    resultsStatus.AppendChild(element);
                    //创建Description
                    element = doc.CreateElement("Description");
                    element.InnerText = msg;
                    resultsStatus.AppendChild(element);
                    //创建PassKey 如果用户登录失败 将加载空字符
                    element = doc.CreateElement("PassKey");
                    element.InnerText = key;
                    resultsStatus.AppendChild(element);
                    results.AppendChild(resultsStatus);
                    //END 创建 ResultsStatus 
    
                    //创建 DataList  数据集合
                    if (status)
                    {
                        results.AppendChild(dataList);
                    }
                    //END 创建一个根节点 KYTResults
                    doc.AppendChild(results);
    
    
                    string path = Server.MapPath("/ws/xsd/ReceiveReturn.xsd");
    
    //验证xml格式是否正确 通过xsd验证
                        string error = "";
                        //声明XmlSchema
                        XmlSchemaSet schemas = new XmlSchemaSet();
                        schemas.Add("", XmlReader.Create(path));
                        //声明事件处理方法
                        ValidationEventHandler eventHandler = new ValidationEventHandler(delegate(object sender, ValidationEventArgs e) {
                            switch (e.Severity)
                            {
                                case XmlSeverityType.Error:
                                    error += e.Message;
                                    break;
                                case XmlSeverityType.Warning:
                                    break;
                            }
                        });
                        doc.Schemas = schemas;
                        //验证xml
                        doc.Validate(eventHandler);
                        //检查是否有异常 如果格式不正确就抛出来
                        if (!"".Equals(error))
                        {
                            throw new Exception(error);
                        }
    

     

    展开全文
  • 方法一: 我使用XmlReader来处理,因为觉得XmlReader简单点。...XML 文件(a.xml): <?xml version="1.0" encoding="utf-8" ?> <PROFILE> <PROFILEID>0001</PROFILEID&...

    方法一:

    我使用XmlReader来处理,因为觉得XmlReader简单点。

    我自己做了个例子来说明,例子里的XML不是很复杂,没有子节点的。

    XML 文件(a.xml):

    <?xml version="1.0" encoding="utf-8" ?>
    <PROFILE>
      <PROFILEID>0001</PROFILEID>
      <DESCIPTION>我的内容</DESCIPTION>
      <URL><![CDATA[http://www.bclc.com/app/DidYouWin/WinningNumbers/Lotto649Print.asp?YEAR=2000&MONTH=6&DRAW_DATE=&GAMEID=1]]></URL>
      <NUMBERS>6</NUMBERS>
      <BONUS>true</BONUS>
    </PROFILE>

    XSD文件(a.xsd):

    <?xml version="1.0" encoding="utf-16"?>
    <xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="PROFILE">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="PROFILEID" type="xsd:int" />
            <xsd:element name="DESCIPTION" type="xsd:string" />
            <xsd:element name="URL" type="xsd:string" />
            <xsd:element name="NUMBERS" type="xsd:int" />
            <xsd:element name="BONUS" type="xsd:boolean" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>

    C#使用xsd文件验证XML 格式是否正确源码:

    XmlReaderSettings st = new XmlReaderSettings();
    XmlSchemaSet schemaSet = new XmlSchemaSet();
    schemaSet.Add(null, spath);
    //st.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler);
    st.ValidationFlags = st.ValidationFlags | XmlSchemaValidationFlags.ReportValidationWarnings;
    st.ValidationType = ValidationType.Schema;
    st.Schemas.Add(schemaSet, @"a.xsd"); 
    
    //设置验证xml出错时的事件。 st.ValidationEventHandler += (obj, e)=>{ //这里加上你的信息处理。 }; 
    XmlReader xr = XmlReader.Create(@"a.xml", st); 
    while (xr.Read()) 
    { 
      if (xr.IsStartElement()
        { xr.Read(); } 
    } 
    xr.Close();

     

     

    方法二:

    //创建xmlDocument 
                XmlDocument doc = new XmlDocument(); 
       
    //创建声明段 如<?xml version="1.0" encoding="utf-8" ?>  
                    doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null)); 
                    //创建一个根节点 KYTResults 
                    XmlElement results = doc.CreateElement("KYTResults"); 
       
                    //创建 ResultsStatus 
                    XmlNode resultsStatus = doc.CreateElement("ResultsStatus"); 
                    //创建Level 
                    XmlElement element = doc.CreateElement("Level"); 
                    element.InnerText = status ? "0" : "1"; 
                    resultsStatus.AppendChild(element); 
                    //创建Description 
                    element = doc.CreateElement("Description"); 
                    element.InnerText = msg; 
                    resultsStatus.AppendChild(element); 
                    //创建PassKey 如果用户登录失败 将加载空字符 
                    element = doc.CreateElement("PassKey"); 
                    element.InnerText = key; 
                    resultsStatus.AppendChild(element); 
                    results.AppendChild(resultsStatus); 
                    //END 创建 ResultsStatus  
       
                    //创建 DataList  数据集合 
                    if (status) 
                    { 
                        results.AppendChild(dataList); 
                    } 
                    //END 创建一个根节点 KYTResults 
                    doc.AppendChild(results); 
       
       
                    string path = Server.MapPath("/ws/xsd/ReceiveReturn.xsd"); 
       
    //验证xml格式是否正确 通过xsd验证 
                        string error = ""; 
                        //声明XmlSchema 
                        XmlSchemaSet schemas = new XmlSchemaSet(); 
                        schemas.Add("", XmlReader.Create(path)); 
                        //声明事件处理方法 
                        ValidationEventHandler eventHandler = new ValidationEventHandler(delegate(object sender, ValidationEventArgs e) { 
                            switch (e.Severity) 
                            { 
                                case XmlSeverityType.Error: 
                                    error += e.Message; 
                                    break; 
                                case XmlSeverityType.Warning: 
                                    break; 
                            } 
                        }); 
                        doc.Schemas = schemas; 
                        //验证xml 
                        doc.Validate(eventHandler); 
                        //检查是否有异常 如果格式不正确就抛出来 
                        if (!"".Equals(error)) 
                        { 
                            throw new Exception(error); 
                        } 

    引文链接:

    C#使用XSD来验证XML。

    C# 使用xsd文件验证XML 格式是否正确

    XML校验学习之一:使用XSD模式验证XML数据

    简介C#读取XML的两种方式

    两篇重点推荐的博文:

    XmlSpy / XSD 以及 验证

    XML学习之Xml Schema:一、Xml Schema概述

    推荐两个XML学习网站:

    在 w3school,你可以找到你所需要的所有的网站建设教程。

    W3China.ORG中国万维网联盟

    转载于:https://www.cnblogs.com/rainbow70626/p/4743780.html

    展开全文
  • XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD),作用是定义 XML 文档的合法构建模块,类似 DTD,但更加强大。作用有: ①定义可出现在文档中的元素 ②定义可出现在文档中的属性 ③定义哪个...
    XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD),作用是定义 XML 文档的合法构建模块,类似 DTD,但更加强大。
    
    作用有:
    ①定义可出现在文档中的元素
    ②定义可出现在文档中的属性
    ③定义哪个元素是子元素
    ④定义子元素的次序
    ⑤定义子元素的数目
    ⑥定义元素是否为空,或者是否可包含文本
    ⑦定义元素和属性的数据类型
    ⑧定义元素和属性的默认值以及固定值

    XSD元素可分为简单元素和复杂元素。

    一、简单元素
    简易元素指仅包含文本的元素,它不会包含任何其他的元素或属性。
    例如XML文档:
    <Name>张三</Name>
    用XSD可写为
    <xs:element name="Name" type="xs:string"/>
    此处“Name”是元素的名称,“xs:string”是XML Schema内建的数据类型。
    最常用的类型有: xs:string、xs:decimal、xs:integer、xs:boolean、xs:date、xs:time等。

    如果要指定元素的默认值或固定值,默认值用 default定义,固定值用 fixed定义。
    <xs:element name="Name" type="xs:string" default="张三"/>
    <xs:element name="Name" type="xs:string" fixed="张三"/>

    也可限定元素的取值范围,例如限定字符串的长度为2至4,则可写为如下:
    <xs:element name="Name">
         <xs:simpleType>
              <xs:restriction base="xs:string">
                    <xs: minLength value="2" />
                     <xs: maxLength value="4" />
               </xs:restriction>
         </xs:simpleType>
    </xs:element>

    也可以写成:
    <xs:element name="Name" type="tns:T_Name" />

    <xs:simpleType name="T_Name">
        <xs:restriction base="xs:string">
          <xs:minLength value="2" />
          <xs:maxLength value="4" />
        </xs:restriction>
    </xs:simpleType>

    二、复合元素
    复合元素包含了其他的元素及/或属性。
    有四种类型的复合元素:
    ①空元素
    ②包含其他元素的元素
    ③仅包含文本的元素
    ④包含元素和文本的元素

    一个XML代码:
     <Person id="1">
         <Name>张三</Name>  
         <Age>120</Age>  
      </Person>  

    在 XML Schema 中,可定义为:
    <xs:element name="Person" maxOccurs="unbounded">
         <xs:complexType>
                <xs:sequence>
                  <xs:element name="Name" type="xs:string" />
                  <xs:element name="Age" type="xs:string" />               
                </xs:sequence>
                <xs:attribute name="id" type="xs:int" use="required" />
        </xs:complexType>
    </xs:element>

    一些用法说明:
    1、maxOccurs="unbounded"
    maxOccurs为Occurrence 指示器,Occurrence 指示器用于定义某个元素出现的频率,有2种类型分别是 maxOccurs和minOccurs ,默认值均为 1。
    maxOccurs 指示器: 规定某个元素可出现的最大次数
    如<xs:element name="Name" type="xs:string" maxOccurs="10"/>
    规定元素“Name”最少出现一次(其中 minOccurs 的默认值是 1),最多出现 10 次。
    minOccurs 指示器: 规定某个元素能够出现的最小次数
    如<xs:element name="Name" type="xs:string" minOccurs="0" maxOccurs="10"/>
    规定元素“Name”最少出现0次,最多出现 10 次。

    提示:如需使某个元素的出现次数不受限制,使用 maxOccurs=" unbounded" 这个声明
    上面<xs:element name="Person" maxOccurs="unbounded">表示元素“Person”出现次数最少一次(其中 minOccurs 的默认值是 1),并且任意次数。

    2、<xs:sequence>
    <xs:sequence>为“Order 指示器”,Order 指示器用于定义元素的顺序,有3种类型分别是All、Choice、Sequence。
    all 指示器: 规定子元素可以按照任意顺序出现
    Choice 指示器: 规定可出现某个子元素或者可出现另外一个子元素(非此即彼)
    Sequence 指示器: 规定子元素必须按照特定的顺序出现

    上面例子代码用<xs:sequence>规定了子元素“Name”和“Age”必须按顺序出现。

    3、use="required"
    use 指示如何使用属性,有3种:
    optional 属性是可选的并且可以具有任何值。这是默认设置。
    <xs:attribute name="id" type="xs:int"/>等价于
    <xs:attribute name="id" type="xs:int" use="optional"/>
    可验证通过<Person>或<Person id="1">
    required 属性必须出现一次
    <xs:attribute name="id" type="xs:int" use="required"/>
    可验证通过<Person id="1">
    prohibited 不能使用属性。
    <xs:attribute name="id" use="prohibited"/>
    规定了不能使用id的属性。


    实例1(无引用命名空间):

    Persons.xml
    <?xml version="1.0" encoding="UTF-8"?>  
    <Persons>
      <Person id="1">
         <Name>张三</Name>  
         <Age>120</Age>  
      </Person>  
      <Person id="2">  
         <Name>李四</Name>  
         <Age>20</Age>  
      </Person>
    </Persons>
    Persons.xsd
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    
      <xs:annotation>
        <xs:documentation> 本文档定义Persons.xml的格式 </xs:documentation>
      </xs:annotation>
      <xs:element name="Persons">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Person" maxOccurs="unbounded">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Name">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:minLength value="2" />
                        <xs:maxLength value="4" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                  <xs:element name="Age">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:pattern value="[1-9][0-9]?|1[01][0-9]|120" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                </xs:sequence>      
                <xs:attribute name="id" use="required">
                   <xs:simpleType>
                    <xs:restriction base="xs:int"></xs:restriction>
                  </xs:simpleType>
                </xs:attribute>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    
    </xs:schema>

    实例2(有引用命名空间):

    Persons2.xml
    <?xml version="1.0" encoding="UTF-8"?>  
    <Persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.xxx.com/XxxSystem">
      <Person id="1">
         <Name>张三</Name>  
         <Age>120</Age>  
      </Person>  
      <Person id="2">  
         <Name>李四</Name>  
         <Age>20</Age>  
      </Person>
    </Persons> 
    Persons2.xsd
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema elementFormDefault="qualified"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:tns="http://www.xxx.com/XxxSystem" >
    
      <xs:annotation>
        <xs:documentation> 本文档定义Persons.xml的格式 </xs:documentation>
      </xs:annotation>
    
      <xs:annotation>
        <xs:documentation>姓名</xs:documentation>
      </xs:annotation>
      <xs:simpleType name="T_Name">
        <xs:restriction base="xs:string">
          <xs:minLength value="2" />
          <xs:maxLength value="4" />
        </xs:restriction>
      </xs:simpleType>
      <xs:annotation>
        <xs:documentation>年龄</xs:documentation>
      </xs:annotation>
      <xs:simpleType name="T_Age">
        <xs:restriction base="xs:string">
          <xs:pattern value="[1-9][0-9]?|1[01][0-9]|120" />
        </xs:restriction>
      </xs:simpleType>
      <xs:annotation>
        <xs:documentation>属性ID</xs:documentation>
      </xs:annotation>
      <xs:simpleType name="T_id">
        <xs:restriction base="xs:int"></xs:restriction>
      </xs:simpleType>
    
      <xs:element name="Persons">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Person" maxOccurs="unbounded" type="tns:T_Person"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    
      <xs:complexType name="T_Person">
        <xs:sequence>
          <xs:element name="Name" type="tns:T_Name" />
          <xs:element name="Age" type="tns:T_Age" />
        </xs:sequence>
        <xs:attribute name="id" type="tns:T_id" use="required" />
      </xs:complexType>
    
    </xs:schema>

    C# 使用xsd文件验证XML文件格式
    /// <summary>
            /// 通过xsd验证xml格式是否正确,正确返回空字符串,错误返回提示
            /// </summary>
            /// <param name="xmlFile">xml文件</param>
            /// <param name="xsdFile">xsd文件</param>
            /// <param name="namespaceUrl">命名空间,无则默认为null</param>
            /// <returns></returns>
            public static string XmlValidationByXsd(string xmlFile, string xsdFile, string namespaceUrl = null)
            {
                StringBuilder sb = new StringBuilder();          
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;
                settings.Schemas.Add(namespaceUrl, xsdFile);
                settings.ValidationEventHandler += (x,y)=>
                {
                    sb.AppendFormat("{0}|", y.Message);
                };
                using (XmlReader reader = XmlReader.Create(xmlFile, settings))
                {
                    try
                    {
                        while (reader.Read());                  
                    }
                    catch (XmlException ex)
                    {
                        sb.AppendFormat("{0}|", ex.Message);
                    }
                }
                return sb.ToString();
            }


    2013-10-17备注

    上面代码是验证xml文件,如果是xml字符串,则可用下面:

     /// <summary>
            /// 通过xsd验证xml格式是否正确,正确返回空字符串,错误返回提示
            /// </summary>
            /// <param name="xmlText">xml文本内容</param>
            /// <param name="schemaFile">xsd文件</param>
            /// <returns></returns>
            public static string XmlValidateByXsd(string xmlText, string schemaFile)
            {
                StringBuilder sb = new StringBuilder();
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;
                settings.Schemas.Add(null, schemaFile);
                settings.ValidationEventHandler += (x, y) =>
                {
                    sb.AppendFormat("{0}\n", y.Message);
                };
                using (XmlReader reader = XmlReader.Create(new StringReader(xmlText), settings))
                {
                    try
                    {
                        while (reader.Read()) ;
                    }
                    catch (XmlException ex)
                    {
                        sb.AppendFormat("{0}\n", ex.Message);
                    }
                }
                return sb.ToString();
            }


    展开全文
  • conf.xsd--------------------------------- <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3school.com.cn" xmlns=...
  • Java运行时库支持验证。...您可能应该使用javax.xml.validation.Validator.importjavax.xml.XMLConstants;importjavax.xml.transform.Source;importjavax.xml.transform.stream.StreamSource;importjav...
  • Xsd文件验证xml的java实现

    千次阅读 2009-04-09 09:28:00
    xsd文件验证xml的java实现 今天在修改数据交换平台时候,看见其中使用此技术 import java.io.File; import java.io.IOException; import javax.xml.transform.Source; import javax.xml.transform.stream....
  • JAVA使用JAXB框架根据schema.xsd文件验证XML合法性 作者:不详 出处:网络转载 2011/7/8 17:31:07 阅读 366 次  JAXB出来很长时间了,英文是Java Architecture for XML Binding, JAVA XML 绑定体系结构。 ...
  • 使用架构(XSD)验证XML文件 假使说XML是一个数据库,那么XSD就是这个数据库的结构。由此可见,XSD是如此重要,如果没有它,我们如何声明以及验证我们需要的XML数据文件的格式和合法性呢?那是不可能完成的任务,...
  • xml 文件与xsd文件的转化以及xsd验证xml的合法性
  • person.xml:(不带命名空间) 张三 120 李四 20 person.xsd: 本文档定义Persons.xml的格式
  • import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import ja
  • xsd文件中包含<xsd:include schemaLocation="CommonData_type.xsd"/>,这样进行xml验证时会因为"CommonData_type.xsd" 没有加载进来出错,如何解决
  • import javax.xml.XMLConstants; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; ...
  • http://bxl766.blog.163.com/blog/static/6109950520111021103653695/import java.io.File; import java.io.IOException; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSourc...
  • 使用XSD验证xml

    千次阅读 2016-08-26 19:33:44
    使用XSD验证XML本文简单介绍如何使用XSD验证自己的xml。项目中我们经常使用xml作为配置或者数据交互的文件格式,在解析XML前最好验证一下或者在生成xml文件后验证一下生成的文件格式是正确,这样可以避免解析时出现...
  • 利用XSD 验证XML文件

    2015-06-10 16:25:56
    XSD文件XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD)。 具体使用方法和定义请参看: http://www.w3school.com.cn/schema/index.asp java自jdk1.5以上新增了SchemaFactory类 ...
  • xsd文件 有许多工具可用于根据XSD验证XML文档。 其中包括操作系统脚本和工具,例如xmllint ,XML编辑器和IDE,甚至是在线验证器。 由于前面提到的方法的局限性或问题,我发现拥有自己的易于使用的XML验证工具很有用...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 40,559
精华内容 16,223
关键字:

如何用xsd文件验证xml