Spring JAXB - 使用模式验证解组 XML 文档

发布于 2025-01-04 19:15:15 字数 1362 浏览 2 评论 0原文

我正在尝试弄清楚如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留一抹残留的笑 2025-01-11 19:15:15
  • 据我所知,JAXB 不会解析 xsi 属性来取消引用 XSD、加载它并用于验证。也许这样做是为了禁用自动验证,否则关闭它会出现问题:)
  • 显然添加了 Spring Jaxb2Marshaller 来实现相同的接口 org.springframework.oxm.Marshaller > (也由 CastorMarshallerJibxMarshaller 实现)。它非常强大,允许您以非常灵活的方式调整 JAXBContext(我无法想象提供的 API 不够时的场景)。从模式的角度来看,新的 Jaxb2Marshaller 是一个构建器,因此它不会向核心 JAXB 功能添加任何内容。但也有一些明显的优势。例如,模式加载非常简单。在本文中,Spring 上下文引用了 person.xsd ()需要明确地投入资源。然后,JAXB 编组器/解组器将在生成/加载 XML 时使用此模式来验证 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),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文