【发布时间】: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.index 一无所知。有必要吗?它应该是什么样子?
【问题讨论】:
-
有什么问题(异常)?你期望哪个输出?我们需要更多细节。
-
我建议把 jbe.printStackTrace();在 catch 块中...
-
我很想看到任何效果 - 当我使用 java 命令或 XML 文件执行控制台时。我什至不确定它在示例中应该如何工作(没有写)
-
是否使用相同的xml架构生成
GreetingListType? -
是的,它是使用教程中提供的架构上的一个 xjc 命令生成的。它应该自己写在控制台吗?
标签: java jaxb marshalling