[英]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"));
I would appreciate any more examples showing Spring/REST with unmarshalling with schema validation. 我将不胜感激任何其他示例,这些示例显示了通过架构验证取消编组的Spring / REST。
Thanks 谢谢
xsi
attribute to dereference XSD, load it and use for validation. xsi
属性来解除对XSD的引用,加载它并用于验证。 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 interface org.springframework.oxm.Marshaller
(which is implemented also by CastorMarshaller
, JibxMarshaller
, ...). Jaxb2Marshaller
来实现相同的接口org.springframework.oxm.Marshaller
(也由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). 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. person.xsd
( <property name="schema" value="classpath:schema/person.xsd"/>
) which one need to put into resources explicitly. 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. @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. RestTemplate
is very simple. 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
. @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.