在解析XML时拦截Xstream [英] Intercepting Xstream while parsing XML

查看:145
本文介绍了在解析XML时拦截Xstream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的简单Java类:

Suppose I have a simple Java class like this:

public class User {

    String firstName;
    String lastName;

    public String getFirstName() {
        return firstName;
    }

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

    public String getLastName() {
        return lastName;
    }

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

}

现在,假设我要解析以下XML:

Now, suppose I want to parse the following XML:

<user>
    <firstName>Homer</firstName>
    <lastName>Simpson</lastName>
</user>

我可以在XStream中执行此操作,如下所示:

I can do this with no problems in XStream like so:

User homer = (User) xstream.fromXML(xml);

好的,到目前为止一切都很好,但这是我的问题。

Ok, all good so far, but here's my problem.

假设我要解析以下XML:

Suppose I have the following XML that I want to parse:

<user>
    <fullName>Homer Simpson</fullName>
</user>

如何使用XStream将此XML转换为相同用户对象?

How can I convert this XML into the same User object using XStream?

我想要一种实现某种回调的方法,这样当XStream解析fullName字段时,我可以将字符串拆分为两个并手动设置第一个名称和用户对象上的姓氏字段。这可能吗?

I'd like a way to implement some kind of callback so that when XStream parses the fullName field, I can split the string in two and manually set the first name and last name fields on the user object. Is this possible?

请注意,我不是要问如何将字符串分成两部分(这是简单的部分),我想知道如何拦截XML解析所以XStream不会尝试在User对象上反射性地设置fullName字段(显然不存在)。

Note that I'm not asking how to split the string in two (that's the easy part), I want to know how to intercept the XML parsing so XStream doesn't try to reflectively set the fullName field on the User object (which obviously doesn't exist).

我查看了XStream提供的转换器但是没有不知道如何将它用于此目的。

I looked at the converters that XStream provides but couldn't figure out how to use it for this purpose.

任何帮助都将不胜感激。

Any help would be appreciated.

推荐答案

您需要一个自定义转换器:

You need a 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 UserConverter implements Converter
{

    @Override
    public boolean canConvert(Class clazz) {
        return clazz.equals(User.class);
    }

    @Override
    public void marshal(Object value, HierarchicalStreamWriter writer,
            MarshallingContext context) 
    {

    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader,
            UnmarshallingContext context) 
    {
        User user = new User();

        reader.moveDown();
        if ("fullName".equals(reader.getNodeName()))
        {
            String[] name = reader.getValue().split("\\s");
            user.setFirstName(name[0]);
            user.setLastName(name[1]);
        }
        reader.moveUp();

        return user;
    }
}

参考: http://x-stream.github.io/converter-tutorial.html

这篇关于在解析XML时拦截Xstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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