xstream 展平一个对象 [英] xstream flatten an object

查看:17
本文介绍了xstream 展平一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用转换器/封送处理来扁平化 xstream 的 xml 输出,但没有运气.例如,

公共类A{公共B b;公共国际F;公共字符串 G;}公共类 B{公共字符串 C;公共字符串 D;公共国际E;}

输出为

<B><C></C><D></D><E></E></B><F></F><G></G></A>

但我需要

<C></C><D></D><E></E><F></F><G></G></A>

这可能吗?如何摆脱B?(C、D、E 是唯一命名的).谢谢.到目前为止,我的尝试是

<预><代码>...public void marshal(对象值,HierarchicalStreamWriter writer,编组上下文上下文){B b = (B) 值;writer.startNode("C");writer.setValue(b.getC());writer.endNode();writer.startNode("D");writer.setValue(b.getD());writer.endNode();writer.startNode("E");writer.setValue(b.getE());writer.endNode();}

解决方案

根据您对 XStream 的依赖程度,您可以在 EclipseLink MOXy 使用@XmlPath 注释:

公共类A{@XmlPath(".") public B b;公共国际F;公共字符串 G;}公共类 B{公共字符串 C;公共字符串 D;公共国际E;}

有关 MOXy 基于 XPath 的映射的信息,请参阅:

I am trying to flatten the xml output of xstream using a converter/marshaling with no luck. For example,

public class A{
    public B b;
    public int F;
    public String G; 
}

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

is output as

<A>
  <B>
     <C></C>
     <D></D>
     <E></E>
  </B>
  <F></F>
  <G></G>
</A>

but I need

<A>
  <C></C>
  <D></D>
  <E></E>
  <F></F>
  <G></G>
</A>

is this possible? How to get rid of B? (C, D, E are uniquely named). Thanks. My attempt thus far has been

...    
public void marshal(Object value, HierarchicalStreamWriter writer,
    MarshallingContext context)
{
    B b = (B) value;
    writer.startNode("C");
    writer.setValue(b.getC());
    writer.endNode();

    writer.startNode("D");
    writer.setValue(b.getD());
    writer.endNode();

    writer.startNode("E");
    writer.setValue(b.getE());
    writer.endNode();
}

解决方案

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:

这篇关于xstream 展平一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆