Spring JAXB - 使用模式验证解组 XML 文档
我正在尝试弄清楚如何将 XML 文档解组为 Java 文档。 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 ">
有一个架构文件,其顶部如下所示:
<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 对象。我不知道如何去做,所以我在谷歌上寻找示例并想出了这个 http://thoughtforge.net/610/marshalling-xml-with-spring-ws-and-jaxb/
我了解其中的大部分内容,除了模式的使用方式或位置。
我见过其他显式指定架构的示例,即
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 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">
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"));
- How is the schema shown in the first link used to validate the xml document?
- Are there any disadvantages to using Spring's jaxb2Marshaller as opposed to direct use of JAXB?
- What is the effect of having the namespace next to the XmlElement annotation? (See the Person class)
I would appreciate any more examples showing Spring/REST with unmarshalling with schema validation.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Jaxb2Marshaller
来实现相同的接口org.springframework.oxm.Marshaller
> (也由CastorMarshaller
、JibxMarshaller
实现)。它非常强大,允许您以非常灵活的方式调整 JAXBContext(我无法想象提供的 API 不够时的场景)。从模式的角度来看,新的Jaxb2Marshaller
是一个构建器,因此它不会向核心 JAXB 功能添加任何内容。但也有一些明显的优势。例如,模式加载非常简单。在本文中,Spring 上下文引用了person.xsd
(
)需要明确地投入资源。然后,JAXB 编组器/解组器将在生成/加载 XML 时使用此模式来验证 XML。@XmlElement(..., namepsace="xxx")
将自动生成具有指定命名空间的 XML 元素。如果有人不使用命名空间,这种情况很少见。我想说在没有命名空间的情况下编写 XSD 是不正常的,因为您想避免元素名称冲突。RestTemplate
结合使用非常简单。您需要确保 JAXB 运行时位于您的类路径中(JDK 6 已经有它)并且您的 bean 使用@XmlRootElement
进行注释。然后只需使用Person person =restTemplate.getForObject(restServiceUrl, Person.class),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 :)Jaxb2Marshaller
was obviously added to implement the same interfaceorg.springframework.oxm.Marshaller
(which is implemented also byCastorMarshaller
,JibxMarshaller
, ...). It is very powerful and allows you to tuneJAXBContext
in very flexible way (I can't imagine the scenario when provided API is not enough). From pattern point of newJaxb2Marshaller
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 theperson.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.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 usePerson person = restTemplate.getForObject(restServiceUrl, Person.class)
,