如何在JAXB(编组)中获得输出?

时间:2011-07-13 17:21:07

标签: java jaxb marshalling

我从here尝试了hello world示例,我在程序中看不到任何输出(在使用“java”命令时在控制台中)。我做错了吗?编组函数的代码如下所示:

public void marshal() {
    try {
        JAXBElement<GreetingListType> gl =
            of.createGreetings( grList );
        JAXBContext jc = JAXBContext.newInstance( "hello" );
        Marshaller m = jc.createMarshaller();
        m.marshal( gl, System.out );
    } catch( JAXBException jbe ){
        // ...
    }
}

我尝试过也尝试将输出放到这样的文件中:

public void marshal() {
    try {
        JAXBElement<GreetingListType> gl =
            of.createGreetings( grList );
        JAXBContext jc = JAXBContext.newInstance( "hello" );
        FileOutputStream fos = new FileOutputStream("plik.xml");
        Marshaller m = jc.createMarshaller();
        //m.marshal( gl, System.out );
        m.marshal(gl, fos);
        fos.close();
    } catch( JAXBException jbe ){
        // ...
    }
  catch( IOException ioe ){
    // ...
}
}

但它没有成功。你有任何解决方案吗?

编辑:在打印Stack Trace后,它给了我这个,看起来很有希望:

javax.xml.bind.JAXBException: "hello" doesnt contain ObjectFactory.class or jaxb.index
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:186)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:148)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:310)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:392)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:357)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:264)
    at Hello.marshal(Hello.java:28)
    at Hello.main(Hello.java:43)

我有ObjectFactory,但对jaxb.in​​dex一无所知。有必要吗?它应该是什么样的?

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题。原来你需要改变这一行:

JAXBContext jc = JAXBContext.newInstance( "hello" );

但是放置生成文件的实际完整包:

JAXBContext jc = JAXBContext.newInstance( "my.generated.package.etc" ); // or whatever.

答案 1 :(得分:1)

该演示似乎不完整(缺少主要方法):

public static void main(String[] args) {
    Hello hello = new Hello();
    hello.make("FOO", "BAR");
    hello.marshal();
}

以下是更正后的版本:

package hello;

import javax.xml.bind.*;

public class Hello {

    private ObjectFactory of;
    private GreetingListType grList;

    public Hello(){
        of = new ObjectFactory();
        grList = of.createGreetingListType();
    }

    public static void main(String[] args) {
        Hello h = new Hello();
        h.make( "Bonjour, madame", "fr" ); 
        h.make( "Hey, you", "en" ); 
        h.marshal();    }

    public void make( String t, String l ){
        GreetingType g = of.createGreetingType();
        g.setText( t );
        g.setLanguage( l );
        grList.getGreeting().add( g );
    }

    public void marshal() {
        try {
            JAXBElement<GreetingListType> gl =
                of.createGreetings( grList );
            JAXBContext jc = JAXBContext.newInstance( "hello" );
            Marshaller m = jc.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.marshal( gl, System.out );
        } catch( JAXBException jbe ){
            // ...
        }
    }

}

<强>输出

<?xml version="1.0" encoding="UTF-8"?>
<Greetings>
   <Greeting language="fr">
      <Text>Bonjour, madame</Text>
   </Greeting>
   <Greeting language="en">
      <Text>Hey, you</Text>
   </Greeting>
</Greetings>