我正在尝试使用一个简单的 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());
}
}`
最后我从一个主要调用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 类中所做的事情?
我已经阅读了关于别名的教程,并认为我的理解是正确的?
感谢您提前提供任何帮助。