XStream 和解析 xml 属性 [英] XStream and parse xml attributes

查看:38
本文介绍了XStream 和解析 xml 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 xml 看起来像:

I have some xml that looks like:

<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;
}

我面临的问题是有重复的 标签导致 xstream 抛出异常.我正在寻找一种方法来从 item<中提取 idfirst-namelast-name 等属性/code> 元素

The issue I am facing is that there are duplicate <item> tags causing xstream to throw an exception. I am looking for a way to pull out the id,first-name,last-name, etc attributes from the item element

推荐答案

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

You need to change the structure of your classes based on the XML provided:

文档:

@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),它将所有 标签转换为 Items 对象中的字段.

Provide a custom converter(ItemsConverter) which converts all the <item> tags to fields in Items object.

自定义转换器:

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>)));
    }
}

这篇关于XStream 和解析 xml 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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