0

I am trying to use a simple xml file to store data. I know its overkill but I thought I could learn a bit about xml at the same time.

I am trying to read the value 1, in the following xml file:

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

The getter/ setter class for the xml data is:

`package com.InvoiceToAccounts.swt;

     public class XML_Log {
     public String invoiceNo;

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

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

My class to read it is:

`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());

    }
}`

And finally I call the xml reader from a main with:

//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);

My problem is the output that last_Invoice_No receives is:

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

Therefore I assume it is something I am doing in the XML_Reader class?

I have read the tutorials on aliases and assumed I had it correct?

Thanks for any help in advance.

4

1 に答える 1

0

com.InvoiceToAccounts.swt.XML_Log@7dccc2

XML_Logこれは、クラスに意味のあるtoString実装を与えていないことを意味します。このことから他に何かを推測することはできません。

このメソッドを XML_Log に追加してみてください。

public String toString() {
    return "XML_Log{ invoiceNo=" + invoiceNo + "}";
}
于 2012-12-19T17:35:57.350 に答える