日期:2014-05-20  浏览次数:20904 次

再遇XStream问题。。。。。。
有这样的javabean
Java code

public class People {
    
    private String sex;
    
    private List userList;
}

public calss User {
    private String name;
    private String address;
}


这样的javabean 用XStream 怎么才能转成这样格式的呢?

XML code
<people>
    <sex>sex</sex>
    <user>
        <name>name</name>
        <address>address</address>
    </user>
    <user>
        <name>name</name>
        <address>address</address>
    </user>
    <user>
        <name>name</name>
        <address>address</address>
    </user>
</people>


或者说是 这样的xml用XStream 怎样才能转成 People 类呢???

------解决方案--------------------
用注解的代码是这样的:
Java code

@XStreamAlias("people")
public class People {
    
    private String sex;
    
    @XStreamImplicit
    private List<User> userList;

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }
}

@XStreamAlias("user")
public class User {
    private String name;
    private String address;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

public class WritePeople {

    public static void main(String[] args) {
        People p = new People();

        p.setSex("male");

        List<User> list = new ArrayList<User>();
        User u = new User();
        u.setName("zhang");
        u.setAddress("beijing");
        list.add(u);
        
        p.setUserList(list);
        
        //Serialize the object
        XStream xs = new XStream();
        xs.processAnnotations(People.class);

        //Write to a file in the file system
        try {
            FileOutputStream fs = new FileOutputStream("people.txt");
            xs.toXML(p, fs);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
    }
}

public class ReadPeople {

    public static void main(String[] args) {
        XStream xs = new XStream(new DomDriver());
        xs.processAnnotations(People.class);
        People p = new People();

        try {
            FileInputStream fis = new FileInputStream("people.txt");
            xs.fromXML(fis, p);

            //print the data from the object that has been read
            System.out.println(p.getUserList().get(0).getName());

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }
}

------解决方案--------------------
1.dto结构如下:
Java code

public class people{

    private  Sex sex;

    private  List<User> user;
    //getter,setter

}

public class Sex{

    private String sex;

    //getter,setter
}


public classs User{

    private String name;

    private String address;

    //getter,setter
}

------解决方案--------------------
探讨

还有这样的一个问题,就是再bean-->xml 的时候,如果两个节点用的是同一个vo,那么生成xml 的时候,第一个节点会有点,当到第二个节点的时候,会出现一个reference 来引用第一个节点,这个要怎么办才能不引用 第一个节点 呢?