编程语言
首页 > 编程语言> > java-如何获取JAXB中的输出(封送处理)?

java-如何获取JAXB中的输出(封送处理)?

作者:互联网

我从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 ){
    // ...
}
}

但没有成功.你有什么解决办法吗?

编辑:打印堆栈跟踪后,它给了我,看起来很有希望:

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一无所知.有必要吗?应该是什么样子?

解决方法:

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

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>

标签:jaxb,marshalling,java
来源: https://codeday.me/bug/20191102/1991284.html