如何使用XStream处理可选属性

时间:2014-11-24 16:31:35

标签: attributes xstream optional

我想知道如何在XStream中实现可选属性。

这是XML:

<AttributeValues>
   <AttributeValue attributeId='id01' languageId='en'>20</AttributeValue>   
   <AttributeValue attributeId='id02'>99</AttributeValue>
   <AttributeValue attributeId='id03' languageId='en'78</AttributeValue>
   <AttributeValue attributeId='id04' languageId='en'>14</AttributeValue>
   <AttributeValue attributeId='id05'>9</AttributeValue>
</AttributeValues>

在解析过程之后,并非每个&lt; AttributeValue&#39; item有一个languageId

XStream将这些空值设置为&#39; null&#39;。但我想把它作为一个空字符串&#34;&#34;。

我该如何实现?

1 个答案:

答案 0 :(得分:1)

您可以使用自定义Converter类来实现目标。

由于您尚未发布AttributeValuesAttributeValue类的代码,因此会有一些猜测。我将使用以下课程:

AttributeValues上课:

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

import java.util.ArrayList;
import java.util.List;

@XStreamAlias("AttributeValues")
public class AttributeValues
{
    @XStreamImplicit(itemFieldName="AttributeValue")
    public List<AttributeValue> Values;

    public AttributeValues()
    {
        Values = new ArrayList<AttributeValue>();
    }

    public void add(String id, String lg, String value)
    {
        AttributeValue item = new AttributeValue();
        item.attributeId = id;
        item.languageId = lg;
        item.value = value;
        Values.add(item);
    }
}

AttributeValue上课:

import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

public class AttributeValue
{
    @XStreamAsAttribute
    public String attributeId;
    @XStreamAsAttribute
    public String languageId;
    public String value;
}

这里是AttributeValueConverter课程:

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class AttributeValueConverter implements Converter
{
    public boolean canConvert(Class clazz) {
        return AttributeValue.class == clazz;
    }

    public void marshal(Object object, HierarchicalStreamWriter hsw, MarshallingContext mc) {
        AttributeValue av = (AttributeValue) object;
        hsw.addAttribute("attributeId", av.attributeId);
        hsw.addAttribute("languageId", av.languageId);
        hsw.setValue(av.value);
    }

    public Object unmarshal(HierarchicalStreamReader hsr, UnmarshallingContext uc) {
        AttributeValue av = new AttributeValue();
        av.attributeId = hsr.getAttribute("attributeId");
        av.languageId = hsr.getAttribute("languageId");
        if (av.languageId == null) av.languageId = "";
        av.value = hsr.getValue();
        return av;
    }

}

序列化:

    AttributeValues obj = new AttributeValues();
    obj.add("id01", "en", "20");

    XStream xstream = new XStream();
    xstream.autodetectAnnotations(true);
    xstream.registerConverter(new AttributeValueConverter());
    String s = xstream.toXML(obj);

反序列化:

    XStream xstream = new XStream();
    xstream.autodetectAnnotations(true);
    xstream.registerConverter(new AttributeValueConverter());
    AttributeValues newobj = (AttributeValues)xstream.fromXML(s);