我的系统既是jibx又是遗留的xml应用程序,我想构建一个构造函数,它可以获取一串xml并将其解组为自己的类。像这样:
public ActiveBankTO(String xmlIn)
{
try
{
ByteArrayInputStream bin = new ByteArrayInputStream(xmlIn.getBytes());
IBindingFactory bfact;
bfact = BindingDirectory.getFactory(ActiveBankTO.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
this = (ActiveBankTO) uctx.unmarshalDocument(bin, null);
} catch (JiBXException e)
{
e.printStackTrace();
}
}
但显然我不能将“this”指定为变量。有没有办法使这项工作?我意识到我可以把它放到一个可以使用的静态方法,或者其他一些技巧来使它工作,但这是以各种形式出现在几个项目上的东西,我想知道这种特殊的方法是否可行。
答案 0 :(得分:2)
不,这是不可能的。静态方法解决方案是最好的主意。
public static ActiveBankTO parseActiveBankTO(String xmlIn) { ActiveBankTO newTO = null; try { ByteArrayInputStream bin = new ByteArrayInputStream(xmlIn.getBytes()); IBindingFactory bfact; bfact = BindingDirectory.getFactory(ActiveBankTO.class); IUnmarshallingContext uctx = bfact.createUnmarshallingContext(); newTO = (ActiveBankTO) uctx.unmarshalDocument(bin, null); } catch (JiBXException e) { e.printStackTrace(); } return newTO; }
答案 1 :(得分:1)
没有。 ti在构造函数中是不可能的。静态工厂方法是唯一真正的方法(你甚至不能在字节码中作弊)。