xstream反序列化返回package.class@7dccc2。我认为我的别名不正确?

时间:2012-12-19 17:31:08

标签: java deserialization alias xstream xml-deserialization

我正在尝试使用简单的xml文件来存储数据。我知道它的矫枉过正,但我​​想我可以同时了解一下xml。

我正在尝试在以下xml文件中读取值1:

`<?xml version="1.0" encoding="UTF-8" standalone="no"?><invoiceNo>1</invoiceNo>`

xml数据的getter / setter类是:

`package com.InvoiceToAccounts.swt;

     public class XML_Log {
     public String invoiceNo;

     public void setNewInvoiceNo(String invoiceNo) {
        this.invoiceNo = invoiceNo;
        }

     public String getNewInvoiceNo() {
        return invoiceNo;
        }   
   }`

我的课程是:

`package com.InvoiceToAccounts.swt;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.thoughtworks.xstream.*;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XML_Reader {

    public String XRead(String logLocation) {

    XStream xs = new XStream(new DomDriver());
    XML_Log only1 = new XML_Log();
    xs.alias("invoiceNo", XML_Log.class);//Alias

try {

    FileInputStream fis = new FileInputStream(logLocation);//e.g."c:/temp/employeedata.txt"

    xs.fromXML(fis, only1);

    //print the data from the object that has been read
    System.out.println(only1.toString());

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        return (only1.toString());

    }
}`

最后我用一个main来调用xml阅读器:

//read log XML_Reader log1 = new XML_Reader(); String last_Invoice_No=log1.XRead("I:\\Invoice_Log.xml"); System.out.println("last_Invoice_no: " + last_Invoice_No);

我的问题是last_Invoice_No收到的输出是:

last_Invoice_no: com.InvoiceToAccounts.swt.XML_Log@7dccc2

因此我认为这是我在XML_Reader类中所做的事情?

我已经阅读了有关别名的教程,并假设我认为它是正确的?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

  

com.InvoiceToAccounts.swt.XML_Log@7dccc2

这意味着您尚未向XML_Log课程提供有意义的toString实施。你不能从中推断出其他任何东西。

尝试添加XML_Log这个方法。

public String toString() {
    return "XML_Log{ invoiceNo=" + invoiceNo + "}";
}