3

特定の形式の XML をアンマーシャリングします。

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

Java オブジェクトをいじった後、別のスキーマを使用する別のサービスに送信したいと考えています。

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

これは JAXB で「簡単に」実行できますか?

4

2 に答える 2

3

JAXB (JSR-222)実装を使用すると、JAXBSourceおよびAPIで XSLT を使用javax.xml.transformして、2 次 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);

完全な例

于 2013-02-12T22:26:38.870 に答える