,我们可以看到XmlBeanDefinitionReader上层是BeanDefinitionReader接口,贺启EnvironmentCapable接口已经父类AbstractBeanDefinitionReader。
我们可以看到上面的步骤分为五步。1:对source进行encode封装,考虑到可液拍罩能需要编码。2:通过sax读取xml文件方式构建一个inputSource。3:getValidationModeForResource获取xml的验证模式。4:加载xml文件,获得对应的Document。5:通过Document注册BeanDefinition。至此就完成了一个xml文件到document,document到BeanDefinition的转换,这里转换使用的sax解析xml文件的方法,有兴趣闹闹的可以debug进去看看。
这有个.java的验证手高方法修改相应路径后可直接运行验证
package test
import java.io.File
import javax.xml.XMLConstants
import javax.xml.parsers.FactoryConfigurationError
import javax.xml.parsers.ParserConfigurationException
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.parsers.DocumentBuilder
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.w3c.dom.Document
public class TestDOMValidation
{
public static void main(String[] args)
{
try
{
// Get Document Builder Factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance()
// Leave off validation, and turn off namespaces
factory.setValidating(false)
factory.setNamespaceAware(false)
DocumentBuilder builder = factory.newDocumentBuilder()
Document doc = builder.parse(new File("data/test/shiporder.xml"))
//Document doc = builder.parse(new File("data/test/simple.xml"))
// SchemaFactory is a schema compiler. It reads external representations of schemas and
//毕槐尺 prepares them for validation.
SchemaFactory constraintFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
// Source: an object that implements this interface contains the information needed to act
/明禅/ as source input (XML source or transformation instructions).
Source constraints = new StreamSource(new File("data/test/shiporder.xsd"))
//Source constraints = new StreamSource(new File("data/test/simple.xsd"))
// Schema object represents a set of constraints that can be checked/ enforced against an
// XML document.
Schema schema = constraintFactory.newSchema(constraints)
// Validator is a processor that checks an XML document against Schema.
Validator validator = schema.newValidator()
// Validate the DOM tree
try
{
validator.validate(new DOMSource(doc))
System.out.println("Document validates fine.")
}
catch (org.xml.sax.SAXException e)
{
System.out.println("Validation error: " + e.getMessage())
}
}
catch (ParserConfigurationException e)
{
System.out.println("The underlying parser does not support the requested features.")
}
catch (FactoryConfigurationError e)
{
System.out.println("Error occurred obtaining Document Builder Factory.")
}
catch (Exception e)
{
e.printStackTrace()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)