【问题标题】:How to get output in JAXB (marshaling)?如何在 JAXB(编组)中获得输出?
【发布时间】:2011-07-13 17:21:06
【问题描述】:

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

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

【问题讨论】:

  • 有什么问题(异常)?你期望哪个输出?我们需要更多细节。
  • 我建议把 jbe.printStackTrace();在 catch 块中...
  • 我很想看到任何效果 - 当我使用 java 命令或 XML 文件执行控制台时。我什至不确定它在示例中应该如何工作(没有写)
  • 是否使用相同的xml架构生成GreetingListType
  • 是的,它是使用教程中提供的架构上的一个 xjc 命令生成的。它应该自己写在控制台吗?

标签: java jaxb marshalling


【解决方案1】:

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

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

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

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

【讨论】:

  • 谢谢..这对我也有用。实际上应该在 newInstance 方法中传递完整的包名称。
【解决方案2】:

演示似乎不完整(缺少 main 方法):

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>

【讨论】:

  • 我用 jbe.printStackTrace(); 将代码粘贴到文件中;在 catch 块中。它给了我错误,我将发布问题。
  • 它有教程页面的主要方法,所以没关系:)
  • @GoMati - 我在示例中没有看到 main 方法:jaxb.java.net/tutorial/…
  • 你说的很对,我加了。不幸的是它没有产生任何结果,但我发布了发生的事情 jbe.printStackTrace();在我的问题中。也许它会缩小真正发生的事情?
  • @GoMati - 你的“hello”包里有什么?您应该有:“GreetingListType”、“GreetingType”和“ObjectFactory”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-27
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 2022-09-27
  • 2011-05-16
相关资源
最近更新 更多