これを行う方法があれば、最もエレガントなものを知りたいです。ここに質問があります: - 抽象クラス Z があると仮定しましょう - Z から継承された 2 つのクラス、A と B という名前があります。
次のように、任意のインスタンス (A または B) をマーシャリングします。
JAXBContext context = JAXBContext.newInstance(Z.class);
Marshaller m = context.createMarshaller();
m.marshal(jaxbObject, ...an outputstream...);
結果の XML で、それがどの種類のインスタンス (A または B) であったかがわかります。
さて、どのように非整列化しますか
JAXBContext jc = JAXBContext.newInstance(Z.class);
Unmarshaller u = jc.createUnmarshaller();
u.unmarshal(...an inputstream...)
UnmarshalException というメッセージが表示されます
"Exception Description: A descriptor with default root element {<my namespace>}<the root tag, e.g. A or B> was not found in the project]
javax.xml.bind.UnmarshalException"
では、Z のインスタンスを取得し、アンマーシャリング後にテストできるようにアンマーシャリングを行うにはどうすればよいでしょうか? たとえば、z instanceof A の場合... z instanceof B の場合、何か他のもの... など。
アイデアや解決策をありがとう。
MOXy を JAXB Impl として JRE1.6 を使用しています。