Spring JAXB-使用架构验证解组XML文档 [英] Spring JAXB - Unmarshalling an XML document with schema validation

查看:80
本文介绍了Spring JAXB-使用架构验证解组XML文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何将XML文档解组和到Java文档. xml文档的顶部看起来像这样

I am trying to work out how to unmarshall and XML document to a Java document. The top of the xml document looks like this

<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">

我想使用Spring/JaxB解组xml文档,并最终将其转换为JPA对象.我不确定该怎么做,所以我在Google上查找了一些示例,并提出了 http://thoughtforge.net/610/marshalling-xml-with-spring-ws-and-jaxb/

I would like to unmarshall the xml document using Spring/JaxB and eventually convert it to a JPA object. 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/

我了解其中的大部分内容,除了使用模式的方式或位置.

I understand most of it except how or where the schema is used.

我还看到了其他示例,其中显式指定了架构,即

I have seen other examples where the schema is explicitly specified, i.e.

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"));

  • 第一个链接中显示的模式如何用于验证xml文档?
  • 与直接使用JAXB相比,使用Spring的jaxb2Marshaller有什么缺点吗?
  • 在XmlElement批注旁边放置名称空间会产生什么影响? (请参见Person类)
  • 我将不胜感激任何其他示例,这些示例显示了通过架构验证取消编组的Spring/REST.

    I would appreciate any more examples showing Spring/REST with unmarshalling with schema validation.

    谢谢

    推荐答案

      据我所知,
    • JAXB不会解析xsi属性来解除对XSD的引用,加载它并用于验证.也许这样做是为了禁用自动验证,否则将其关闭是有问题的:)
    • 显然添加了
    • Spring Jaxb2Marshaller以实现相同的接口org.springframework.oxm.Marshaller(也由CastorMarshallerJibxMarshaller,...实现).它非常强大,并允许您以非常灵活的方式调整JAXBContext(我无法想象提供的API不够时的情况).从新的Jaxb2Marshaller模式开始,它是一个构建器,因此它不会对JAXB核心功能添加任何内容.但是有一些明显的优势.例如,架构加载非常简单.在本文中,Spring上下文引用了person.xsd(<property name="schema" value="classpath:schema/person.xsd"/>),需要将其显式地放入资源中.然后,在生成/加载XML时,JAXB marshaller/unmarshaller将使用此架构来验证XML.
    • @XmlElement(..., namepsace="xxx")将自动生成具有指定名称空间的XML元素.如果有人不使用名称空间,这种情况很少见.我想写没有命名空间的XSD是不正常的,因为要避免元素名称冲突.
    • 将JAXB与RestTemplate一起使用非常简单.您需要确保JAXB运行时在您的类路径中(JDK 6已经拥有它),并且您的bean用@XmlRootElement进行了注释.然后只需使用Person person = restTemplate.getForObject(restServiceUrl, Person.class)
      • As far as I know JAXB does not parse xsi attribute to dereference XSD, load it and use for validation. 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, ...). 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). From pattern point of new Jaxb2Marshaller is a builder, so it does not add anything to core JAXB functionality. 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. Then JAXB marshaller/unmarshaller will use this schema to validate XML when XML is generated/loaded.
      • @XmlElement(..., namepsace="xxx") will automatically generate this XML element with a specified namespace. 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.
      • Using JAXB with RestTemplate is very simple. You need to be sure that JAXB runtime is in your classpath (JDK 6 already has it) and your bean is annotated with @XmlRootElement. Then just use Person person = restTemplate.getForObject(restServiceUrl, Person.class),
      • 这篇关于Spring JAXB-使用架构验证解组XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆