XStream parsing XML message

One, XML data in one format is converted to object.

pom.xmlIntroduce

<!--javaBeanBidirectional conversion of XML and -->< dependency>< groupId> com.thoughtworks.xstream< /groupId>≪ artifactId> xstream< /artifactId>< version>1.4.10</version>
</dependency>

xmlFile template

<?xml version="1.0" encoding="GBK"?>
<MESSAGE>
    <MESSAGEHEAD>
        <MESSAGEID/>
        <SRC/>
    </MESSAGEHEAD>
    <MESSAGEBODY>
        <TRANLIST>
            <TRAN_DATE/>
            <TRAN_TIME/>
        </TRANLIST>
    </MESSAGEBODY>
</MESSAGE>

Background needs to be created

1、 Total Total

class Total
    private static final String headInfo = "<?xml version=\"1.0\" encoding=\"GBK\"?>";

    @XStreamAlias("MESSAGEHEAD")
    private Head head;


    @XStreamAlias("MESSAGEBODY")
    private Body body;
  // getter setter

2、Header information Head

class Head
    @XStreamAlias("MESSAGEID")
    private String messageId;

    @XStreamAlias("SRC")
    private String src;
    
    // getter settter

3、Body,Collection of detail classes

class Body
    @XStreamImplicit(itemFieldName = "TRANLIST")
    private List<Detail> detail;

4、DetailDetail class

class Detail
    @XStreamConverter(value = XStreamDateConverter.class)
    @XStreamAsAttribute
    @XStreamAlias("TRAN_DATE")
    private Date tranDate;

    @XStreamAlias("TRAN_TIME")
    private String tranTime;
  // getter setter

It should be emphasized that String types do not require conversion, and other types can implement conversion formats by implementing the interface SingleValueConverter, writing a conversion to date Date format, and others like this

public class XStreamDateConverter implements SingleValueConverter {

    private final String YMD = "yyyyMMdd";

    SimpleDateFormat dateFormat = new SimpleDateFormat(YMD);

    @Override
    public String toString(Object o) {
        String result = null;
        if (o instanceof Date) {
            Date date = (Date) o;
            result = dateFormat.format(date);
        }
        return result;
    }

    @Override
    public Object fromString(String s) {
        Date date = null;
        if (s != null && !"".equals(s)) {
            try {
                date = dateFormat.parse(s);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

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

The basic work has been completed, and the next step is to transform XML into object entity.

public static Total transXmlToObject(String xmlBody) {
        XStream xStream = new XStream();
        xStream.alias("MESSAGE", Total.class);
        xStream.alias("MESSAGEHEAD", Head.class);
        xStream.alias("MESSAGEBODY", Body.class);
        xStream.alias("TRANLIST", Detail.class);
        xStream.processAnnotations(new Class[]{Total.class, Head.class, Body.class, Detail.class});

        Object object = xStream.fromXML(xmlBody);
        Total data = (Total) object;
        return data;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *