XStream和解析xml属性

时间:2015-12-10 16:01:58

标签: java xml xstream

我有一些看起来像这样的xml:

<document>
    <item name="id">some value</item>
    <item name="first-name">some value</item>
    <item name="last-name">some value</item>
    <item name="address">some value</item>
    <item name="zip">some value</item>
</document>

POJO:

@XStreamAlias("document")
public class Doc{
    private String id;
    private String firstName;
    private String lastName;
    private String address;
    private String zip;
}

编辑:

我面临的问题是有重复的<item>标记导致xstream抛出异常。我正在寻找一种从id元素中提取first-namelast-nameitem等属性的方法

1 个答案:

答案 0 :(得分:2)

您需要根据提供的XML更改类的结构:

文件:

@XStreamAlias("document")
public class Document {
    @XStreamConverter(value = ItemsConverter.class)
    private Items items;
}

产品:

public class Items {
    private String id;
    private String firstName;
    private String lastName;
    private String address;
    private String zip;

    public void setId(String id) {
        this.id = id;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }
}

提供自定义转换器(ItemsConverter),将所有<item>标记转换为Items对象中的字段。

自定义转换器:

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 ItemsConverter implements Converter {


    @Override
    public void marshal(Object o, HierarchicalStreamWriter hierarchicalStreamWriter, MarshallingContext marshallingContext) {
        // implement logic for marshalling to xml
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader, UnmarshallingContext unmarshallingContext) {
        Items items = new Items();
        while (hierarchicalStreamReader.hasMoreChildren()) {
            hierarchicalStreamReader.moveDown();
            final String currentAttribute = hierarchicalStreamReader.getAttribute("name");
            if ("id".equals(currentAttribute)) {
                items.setId(hierarchicalStreamReader.getValue());
            } else if ("first-name".equals(currentAttribute)) {
                items.setFirstName(hierarchicalStreamReader.getValue());
            } else if ("last-name".equals(currentAttribute)) {
                items.setLastName(hierarchicalStreamReader.getValue());
            } else if ("address".equals(currentAttribute)) {
                items.setAddress(hierarchicalStreamReader.getValue());
            } else if ("zip".equals(currentAttribute)) {
                items.setZip(hierarchicalStreamReader.getValue());
            }
            hierarchicalStreamReader.moveUp();
        }
        return items;
    }

    @Override
    public boolean canConvert(Class aClass) {
        return aClass == Items.class;
    }
}

测试类:

public class XStreamTest {

    public static void main(String[] args){
        XStream stream = new XStream();
        stream.processAnnotations(Document.class);
        Document document = (Document)stream.fromXML(new InputStreamReader(XStreamTest.class.getResourceAsStream(<your file name>)));
    }
}