Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将JAXB对象打包到不同的模式?_Java_Xml_Jaxb - Fatal编程技术网

Java 如何将JAXB对象打包到不同的模式?

Java 如何将JAXB对象打包到不同的模式?,java,xml,jaxb,Java,Xml,Jaxb,我将解组一个特定格式的XML,例如 <root> <a/> <b/> <c> <x/> </c> <d/> </root> 在使用Java对象之后,我想将它发送到另一个使用不同模式的服务,例如 <anotherRoot> <a/> <x/> <something> <d/&

我将解组一个特定格式的XML,例如

<root>
   <a/>
   <b/>
   <c>
     <x/>
   </c>
   <d/>
</root>

在使用Java对象之后,我想将它发送到另一个使用不同模式的服务,例如

<anotherRoot>
   <a/>
   <x/>
   <something>
      <d/>
   </something>
</anotherRoot>

这可以通过JAXB“轻松地”完成吗?

使用任何可以在
JAXBSource
javax.xml.transform
API上使用XSLT的实现来生成二级xml结构:

    JAXBContext jc = JAXBContext.newInstance(Foo.class);

    // Output XML conforming to first XML Schema
    Marshaller marshaller = jc.createMarshaller();
    marshaller.marshal(foo, System.out);

    // Create Transformer on Style Sheet that converts XML to 
    // conform the second XML Schema
    TransformerFactory tf = TransformerFactory.newInstance();
    StreamSource xslt = new StreamSource(
            "src/example/stylesheet.xsl");
    Transformer transformer = tf.newTransformer(xslt);

    // Source
    JAXBSource source = new JAXBSource(jc, foo);

    // Result
    StreamResult result = new StreamResult(System.out);

    // Transform
    transformer.transform(source, result);
完整示例


您可以为其他服务创建代理,并将其bean视为简单的数据传输对象

因此,当您希望调用该服务时,您可以根据适当的模型对象(您使用的模型对象,包含业务逻辑的模型对象)的值手动填充bean,并使用bean调用该服务


如果服务接口发生更改,您可以重新创建代理,编译器将帮助您修复转换。

想了解更多详细信息将有助于回答。您尝试了什么?在指定不同的模式时是否尝试编组对象?这似乎是一个快速的尝试可能会回答你的问题。。。另外,也许您可以编写一个方法,将一个java对象转换为另一个对象