XStream and parse xml attributes

笑着哭i 提交于 2019-12-26 06:48:09

问题


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

EDIT:

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


回答1:


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

Document:

@XStreamAlias("document")
public class Document {
    @XStreamConverter(value = ItemsConverter.class)
    private Items 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;
    }
}

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

Custom converter:

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

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


来源:https://stackoverflow.com/questions/34206426/xstream-and-parse-xml-attributes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!