我想用一個簡單的xml文件來存儲數據。我知道它的過度殺傷力,但我認爲我可以同時學習一些關於xml的知識。xstream反序列化返回[email protected]。我假設我的別名不正確?
我想讀的值爲1,在下面的XML文件:
`<?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());
}
}`
最後我所說的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: [email protected]
因此,我認爲它是什麼我在XML_Reader類在做什麼?
我已經閱讀了關於別名的教程,並假設我有它正確嗎?
感謝您提前提供任何幫助。
我確實有一個字符串字符串方法之前,但後來我收到的是一個空。我添加了你的版本來查看並返回:'last_Invoice_no:XML_Log {invoiceNo = null}'謝謝你的幫助:) – user1916403