xstream flatten an object

我们两清 提交于 2019-12-02 09:05:46

Depending on how tied you are to XStream, you can do this quite easily in EclipseLink MOXy using the @XmlPath annotation:

public class A{ 
    @XmlPath(".") public B b; 
    public int F; 
    public String G;  
} 

public class B{ 
    public String C; 
    public String D; 
    public int E; 
} 

For Information on MOXy's XPath based mapping see:

I found a temporary solution, though it is not the best.

If I set my canConvert function to check the surrounding object A instead of B, I can manipulate the entire inner object.

public boolean canConvert(Class c)
{
    return A.class == c;
}

Since I have to define all of class A, this is a lot more work (especially in a real XML object, instead of my contrived example). Does anyone know of a way to get the same result using a Converter on inner class B only?

public boolean canConvert(Class c)
{
    return B.class == c;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!