Spring源码(三) -- Xml文件转换Document

Spring源码(三) -- Xml文件转换Document,第1张

我们上一节探究了ClassPathResource通过输入Path构造出一个Resource对象,我们这一节继续学习XmlBeanFactory如何解析Resource,将Resource转化成一个Document。

,我们可以看到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()

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12292504.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存