简体   繁体   English

Spring JAXB-使用架构验证解组XML文档

[英]Spring JAXB - Unmarshalling an XML document with schema validation

I am trying to work out how to unmarshall and XML document to a Java document. 我正在尝试找出如何将XML文档解组和Java文档。 The top of the xml document looks like this xml文档的顶部看起来像这样

<xs:myData xmlns:xs="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com example.xsd ">

There is a schema file whose top section looks like this: 有一个架构文件,其顶部如下所示:

<schema targetNamespace="http://www.example.com" 
elementFormDefault="qualified"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:xs="http://www.example.com">

I would like to unmarshall the xml document using Spring/JaxB and eventually convert it to a JPA object. 我想使用Spring / JaxB解组xml文档,并最终将其转换为JPA对象。 I am not sure how to go about so i looked for examples on google and came up with this http://thoughtforge.net/610/marshalling-xml-with-spring-ws-and-jaxb/ 我不确定该怎么做,所以我在Google上寻找了一些例子,并提出了http://thoughtforge.net/610/marshalling-xml-with-spring-ws-and-jaxb/

I understand most of it except how or where the schema is used. 我了解其中的大部分内容,除了使用模式的方式或位置。

I have seen other examples where the schema is explicitly specified, ie 我看到了其他示例,其中显式指定了架构,即

SchemaFactory schemaFac = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema sysConfigSchema = schemaFac.newSchema(
                    new File("example.xsd"));
            unmarshaller.setSchema(sysConfigSchema);
            RootElement root = (RootElement)unmarshaller.unmarshal(
                    new File("example1.xml"));
  • How is the schema shown in the first link used to validate the xml document? 第一个链接中显示的模式如何用于验证xml文档?
  • Are there any disadvantages to using Spring's jaxb2Marshaller as opposed to direct use of JAXB? 与直接使用JAXB相比,使用Spring的jaxb2Marshaller有什么缺点吗?
  • What is the effect of having the namespace next to the XmlElement annotation? 在XmlElement批注旁边放置名称空间会有什么影响? (See the Person class) (请参见Person类)

I would appreciate any more examples showing Spring/REST with unmarshalling with schema validation. 我将不胜感激任何其他示例,这些示例显示了通过架构验证取消编组的Spring / REST。

Thanks 谢谢

  • As far as I know JAXB does not parse xsi attribute to dereference XSD, load it and use for validation. 据我所知,JAXB不会解析xsi属性来解除对XSD的引用,加载它并用于验证。 Perhaps that was done to disable automatic validation, otherwise it would be problematic to switch it off :) 也许这样做是为了禁用自动验证,否则将其关闭是有问题的:)
  • Spring Jaxb2Marshaller was obviously added to implement the same interface org.springframework.oxm.Marshaller (which is implemented also by CastorMarshaller , JibxMarshaller , ...). 显然,添加了Spring Jaxb2Marshaller来实现相同的接口org.springframework.oxm.Marshaller (也由CastorMarshallerJibxMarshaller等实现)。 It is very powerful and allows you to tune JAXBContext in very flexible way (I can't imagine the scenario when provided API is not enough). 它非常强大,并且允许您以非常灵活的方式调整JAXBContext (我无法想象提供的API不够时的情况)。 From pattern point of new Jaxb2Marshaller is a builder, so it does not add anything to core JAXB functionality. 从新的Jaxb2Marshaller模式来看, Jaxb2Marshaller是一个构建器,因此它不会对JAXB核心功能添加任何内容。 But there are some evident advantages. 但是有一些明显的优势。 For example, schema loading is very simple. 例如,架构加载非常简单。 In the article the Spring context refers the person.xsd ( <property name="schema" value="classpath:schema/person.xsd"/> ) which one need to put into resources explicitly. 在本文中,Spring上下文引用了person.xsd<property name="schema" value="classpath:schema/person.xsd"/> ),需要将其明确地放入资源中。 Then JAXB marshaller/unmarshaller will use this schema to validate XML when XML is generated/loaded. 然后,当生成/加载XML时,JAXB marshaller / unmarshaller将使用此模式来验证XML。
  • @XmlElement(..., namepsace="xxx") will automatically generate this XML element with a specified namespace. @XmlElement(..., namepsace="xxx")将自动生成具有指定名称空间的XML元素。 It's rare case if somebody does not use namespaces. 如果有人不使用名称空间,这种情况很少见。 I would say writing XSD without namespaces is not normal, as you want to avoid the element name collision. 我想说没有命名空间的XSD是不正常的,因为要避免元素名称冲突。
  • Using JAXB with RestTemplate is very simple. 将JAXB与RestTemplate一起使用非常简单。 You need to be sure that JAXB runtime is in your classpath (JDK 6 already has it) and your bean is annotated with @XmlRootElement . 您需要确保JAXB运行时位于类路径中(JDK 6已经拥有它),并且您的bean用@XmlRootElement注释。 Then just use Person person = restTemplate.getForObject(restServiceUrl, Person.class) , 然后只需使用Person person = restTemplate.getForObject(restServiceUrl, Person.class)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM