繁体   English   中英

Spring jaxb2marshaller-如何基于模式进行封送?

[英]spring jaxb2marshaller - how to marshal based on schema?

我正在尝试学习如何使用Spring将XML编组与Java代码之间的编组。 我的示例代码用于基于XSD文件(用于验证)将XML文件解组到Java类中。

我所坚持的但没有找到太多信息的是如何基于XSD模式文件进行封送?

代码摘录为:

在applicationContext.xml文件中,我有:

<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="packagesToScan" value="com.company" />
    <property name="schema" value="file:test1.xsd" />       
</bean>

我的主类是jaxb2Marshaller bean。

在我的主类中,我要编写XML:

private void writeXML() {
    crashScene.getWorkZoneInfo().getWorkZoneDetails().setWorkZoneSpeedLimit(45);  // updates the data previously read in by unmarshalling.

    FileOutputStream fos;
    try {
        fos = new FileOutputStream("test_output.xml");          
        marshaller.marshal(crashScene, new StreamResult(fos));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (XmlMappingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

由于我在applicationContext中设置了一个模式,因此当尝试编写XML时会出现异常:

org.springframework.oxm.MarshallingFailureException: JAXB marshalling exception; nested exception is javax.xml.bind.MarshalException- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 0; columnNumber: 0; cvc-complex-type.2.4.a: Invalid content was found starting with element 'FormNum'. One of '{XmlSchemaVersion}' is expected.]

如果我尝试在不使用定义的架构的情况下编写XML,则将进行编组工作。

如何根据架构定义写出XML?

非常感谢!

克里斯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM