用xstream进行bean和xml转换出现的问题

最近在做xstream对bean和xml进行转化的小例子,在xml转化成bean时出现一个问题。话不多说,先贴代码:
  package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.thoughtworks.xstream.XStream;
public class XStreamTest {

private XStream xstream=null;
private ObjectOutputStream  out = null;
    private ObjectInputStream in = null;
    private Stuent stu=null;
   
    @Before
    public void init(){
    xstream =new XStream();
    stu =new Stuent();
    stu.setAddress("china");
    stu.setEmail("[email protected]");
    stu.setName("chen");
    stu.setId(1);
    Profile p =new Profile("软件工程师", "15850539386", "XXXXX");
    stu.setProfile(p);
    }
   
    @After
    public void destory() {
        xstream = null;
        stu = null;
        try {
            if (out != null) {
                out.flush();
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.gc();
    }

   
    @Test
    public void writeBean2XML() {
        try {
       
          System.out.println("------------Bean->XML------------");
          System.out.println(xstream.toXML(stu));
          System.out.println("-----------重命名后的XML----------");       
            //类重命名
            xstream.alias("account", Stuent.class); 
            xstream.alias("message", Profile.class);
            //属性重命名
            xstream.aliasField("编号", Stuent.class, "id");
            xstream.aliasField("邮件", Stuent.class, "email");
            xstream.aliasField("姓名", Stuent.class, "name");
            xstream.aliasField("地址", Stuent.class, "address");
            xstream.aliasField("工作", Profile.class, "job");
            xstream.aliasField("电话", Profile.class, "tel");
            xstream.aliasField("备注", Profile.class, "remark");
 
            FileOutputStream ops = new FileOutputStream(new File("d:\\111.xml"));  
        xstream.toXML(stu, ops);  
            ops.close();
            System.out.println(xstream.toXML(stu));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void readXML2Bean(){
    try {  
            FileInputStream ops = new FileInputStream(new File("d:\\111.xml"));  
            xstream.fromXML(ops,stu);  
            stu.getName();
            stu.getId();
            stu.getAddress();
            stu.getEmail();
            stu.getProfile();
            System.out.println(stu.getName());  
            System.out.println(stu.getId());  
            System.out.println(stu.getAddress());  
            System.out.println( stu.getEmail());
            System.out.println( stu.getProfile());     
            ops.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }      
   
   
    }
 
  

我添加了红色字体部分的,对类及属性进行别名处理,不加别名时,xml是可以解析成bean的,加了之后解析就报错了;

com.thoughtworks.xstream.mapper.CannotResolveClassException: account : account
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:49)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:26)
at com.thoughtworks.xstream.mapper.XStream11XmlFriendlyMapper.realClass(XStream11XmlFriendlyMapper.java:23)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:26)
at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:72)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:26)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:26)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:26)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:26)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:26)
at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:60)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:26)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:26)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:26)
at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:76)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:26)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:26)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:26)
at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:34)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:113)
at com.thoughtworks.xstream.core.ReferenceByXPathMarshallingStrategy.unmarshal(ReferenceByXPathMarshallingStrategy.java:29)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:826)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:813)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:796)
at test.XStreamTest.readXML2Bean(XStreamTest.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)



小弟愚钝,还请大家赐教啊~~~
   

你可能感兴趣的:(xstream)