zig*_*ggy 3 java spring xsd jaxb
我正在尝试解决如何将 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 ">
Run Code Online (Sandbox Code Playgroud)
有一个架构文件,其顶部如下所示:
<schema targetNamespace="http://www.example.com"
elementFormDefault="qualified"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:xs="http://www.example.com">
Run Code Online (Sandbox Code Playgroud)
我想使用 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"));
Run Code Online (Sandbox Code Playgroud)
我希望能有更多示例展示 Spring/REST 与架构验证的解组。
谢谢
xsi属性来取消引用 XSD,加载它并用于验证。也许这样做是为了禁用自动验证,否则将其关闭将有问题:)Jaxb2Marshaller显然添加了Spring来实现相同的接口org.springframework.oxm.Marshaller(也由CastorMarshaller, JibxMarshaller, ... 实现)。它非常强大,允许您以JAXBContext非常灵活的方式进行调优(我无法想象提供的 API 不够用的场景)。从模式来看,newJaxb2Marshaller是一个构建器,因此它不会向核心 JAXB 功能添加任何内容。但是有一些明显的优势。例如,模式加载非常简单。在文章中,Spring 上下文指的是person.xsd( <property name="schema" value="classpath:schema/person.xsd"/>) 需要明确地放入资源中。然后 JAXB marshaller/unmarshaller 将在生成/加载 XML 时使用此模式来验证 XML。@XmlElement(..., namepsace="xxx")将使用指定的命名空间自动生成此 XML 元素。如果有人不使用命名空间,这种情况很少见。我会说在没有命名空间的情况下编写 XSD 是不正常的,因为您想避免元素名称冲突。RestTemplate非常简单。您需要确保 JAXB 运行时在您的类路径中(JDK 6 已经有了它)并且您的 bean 用@XmlRootElement. 然后只需使用Person person = restTemplate.getForObject(restServiceUrl, Person.class),| 归档时间: |
|
| 查看次数: |
8615 次 |
| 最近记录: |