如何在编组/解编组java对象时忽略fieldname

如何在编组/解编组java对象时忽略fieldname,java,marshalling,unmarshalling,xstream,ignore,Java,Marshalling,Unmarshalling,Xstream,Ignore,我有以下问题,我不知道如何解决 我有一个基于公共接口的不同类型点的列表。 我正在使用JavaXStream对这些类进行整理和解整理 public static void main(String[] args) { List<IPoint> listOfPoint = new ArrayList<IPoint>(); listOfPoint.add(new PointTypeA(0.1)); listOfPoint.add(new PointTyp

我有以下问题,我不知道如何解决

我有一个基于公共接口的不同类型点的列表。 我正在使用JavaXStream对这些类进行整理和解整理

public static void main(String[] args) {

    List<IPoint> listOfPoint = new ArrayList<IPoint>();
    listOfPoint.add(new PointTypeA(0.1));
    listOfPoint.add(new PointTypeB(0.2));
    listOfPoint.add(new PointTypeA(0.3));
    PointSet ps = new PointSet(1, listOfPoint);

    XStream xstream = new XStream(new StaxDriver());
    xstream.processAnnotations(PointTypeA.class);
    xstream.processAnnotations(PointTypeB.class);
    xstream.processAnnotations(PointSet.class);

    String xml = xstream.toXML(ps);
    System.out.println(xml);
}
publicstaticvoidmain(字符串[]args){
List listOfPoint=new ArrayList();
添加(新的PointTypeA(0.1));
添加(新的PointTypeB(0.2));
添加(新的PointTypeA(0.3));
点集ps=新点集(1,listOfPoint);
XStream XStream=newxstream(new statxdriver());
processAnnotations(PointTypeA.class);
processAnnotations(PointTypeB.class);
processAnnotations(PointSet.class);
字符串xml=xstream.toXML(ps);
System.out.println(xml);
}
以XML格式打印对象时,会得到以下结果:

<set id="1">
  <typeA>
    <xCoordinate>0.1</xCoordinate>
  </typeA>
  <typeB>
    <xCoordinate>0.2</xCoordinate>
  </typeB>
  <typeA>
    <xCoordinate>0.3</xCoordinate>
  </typeA>
</set>

0.1
0.2
0.3
但我希望得到以下输出,而不是上面的结果:

<set id="1">
  <typeA>0.1</typeA>
  <typeB>0.2</typeB>
  <typeA>0.3</typeA>
</set>

0.1
0.2
0.3
我想要的不是像
这样的标记,而是希望它们的值存储在类名的标记下。 我不想忽略xCoordinate字段的值,但我希望有一个“内联值”。 有可能吗? 我尝试过转换器,但没有成功,我不知道如何解决这个问题

我的课程是:

public interface IPoint {

    int getSomeInformation();
}  

@XStreamAlias("set")
public class PointSet {

    @XStreamAsAttribute
    private int id;

    @XStreamImplicit
    private List<IPoint> points;

    public PointSet(int id, List<IPoint> points) {
        super();
        this.id = id;
        this.points = points;
    }
}

@XStreamAlias("typeA")
public class PointTypeA implements IPoint {

    private double xCoordinate;

    public PointTypeA(double d) {
        super();
        this.xCoordinate = d;
    }
}

@XStreamAlias("typeB")
public class PointTypeB implements IPoint {

    private double xCoordinate;

    public PointTypeB(double d) {
        super();
        this.xCoordinate = d;
    }
}
公共接口IPoint{
int getSomeInformation();
}  
@XStreamAlias(“集”)
公共类点集{
@XStreamAsAttribute
私有int-id;
@XStreamImplicit
私人名单点;
公共点集(整数id,列表点){
超级();
this.id=id;
这个点=点;
}
}
@XStreamAlias(“A型”)
公共类PointTypeA实现IPoint{
私人双重协调;
公共点类型A(双d){
超级();
这个.xCoordinate=d;
}
}
@XStreamAlias(“B型”)
公共类PointTypeB实现IPoint{
私人双重协调;
公共点类型B(双d){
超级();
这个.xCoordinate=d;
}
}
如果可以的话,请帮助我。
谢谢。

用于point类的转换器非常简单

public static class CoordConverter implements Converter
{
    public boolean canConvert(Class clazz)
    {
        return PointTypeA.class == clazz;
    }

    public void marshal(Object object, HierarchicalStreamWriter hsw, MarshallingContext mc)
    {
        PointTypeA obj = (PointTypeA) object;
        hsw.setValue(String.valueOf(obj.xCoordinate));
    }

    public Object unmarshal(HierarchicalStreamReader hsr, UnmarshallingContext uc)
    {
        double val = Double.parseDouble(hsr.getValue());
        PointTypeA obj = new PointTypeA(val);
        return obj;
    }
}
你可以在网上注册

xstream.registerConverter(new CoordConverter());

当然,此转换器对
PointTypeA
类有效,但您可以轻松地将上述代码扩展到所需的其他类和/或编写更通用的版本。

用于point类的转换器相当简单

public static class CoordConverter implements Converter
{
    public boolean canConvert(Class clazz)
    {
        return PointTypeA.class == clazz;
    }

    public void marshal(Object object, HierarchicalStreamWriter hsw, MarshallingContext mc)
    {
        PointTypeA obj = (PointTypeA) object;
        hsw.setValue(String.valueOf(obj.xCoordinate));
    }

    public Object unmarshal(HierarchicalStreamReader hsr, UnmarshallingContext uc)
    {
        double val = Double.parseDouble(hsr.getValue());
        PointTypeA obj = new PointTypeA(val);
        return obj;
    }
}
你可以在网上注册

xstream.registerConverter(new CoordConverter());

当然,这个转换器对于
PointTypeA
类是有效的,但是您可以轻松地为其他需要的类扩展上述代码和/或编写更通用的版本。

我根本不知道XStream是如何工作的,但在Java中,通常使用关键字“transient”来表示变量不会自动存储/检索。不要把它和“volatile”混在一起,这表示在缓存方面要读写的变量。我根本不知道XStream是如何工作的,但在Java中通常使用关键字“transient”来表示变量不会自动存储/检索。不要将其与“volatile”btw混淆,后者表示要在缓存方面读写的变量。