这段代码......
BADFM badfmMessage = (BADFM) um.unmarshal(new StringReader(Str));
JAXBElement extends Message> element = badfmMessage.getMessage();......有点令人费解。你从Unmarshaller获得一个Object,将它转换为BADFM,然后从中获取一个JAXBElement?这个BADFM课程是什么?
我已经使用了Message,MobBlock,Ord和Bagger类,将它们放在一个包中,然后在其中创建了这个Main类:
package jaxb1;
import java.io.Reader;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
public class Main {
public static void main(String[] args) throws Exception {
final JAXBContext context =
JAXBContext.newInstance(
Bagger.class,
Ord.class,
MobBlock.class,
Message.class
);
final Unmarshaller unmarshaller = context.createUnmarshaller();
final Reader reader =
new StringReader("<?xml version=\"1.0\"?>\r\n"
+ "\r\n"
+ "\r\n"
+ "\r\n"
+ "\r\n"
+ "\r\n"
+ "\r\n"
+ "");
final Object o = unmarshaller.unmarshal(reader);
final Message m = (Message)o;
System.out.println(m.getMob().getOrd().getBagger().getDSE());
}
}结果很好。运行时输出C。