Java Xstream与继承

Java Xstream与继承,java,xml,inheritance,abstract-class,xstream,Java,Xml,Inheritance,Abstract Class,Xstream,我有一个.dat文件,其中包含某些类型的产品。我正在读取.dat文件,然后将其编写为JSON和XML文件。我将在文件之后在pic中解释这个bug。问题是,在XML文件中没有productCode,而在JSON中则有。我不知道XML中的productCode有什么问题,因为我在这两种产品中都传递了super(productCode) 这个代码怎么了 public static void xmlConverter(List<Product> products) { XStream

我有一个
.dat
文件,其中包含某些类型的产品。我正在读取
.dat
文件,然后将其编写为JSON和XML文件。我将在文件之后在pic中解释这个bug。问题是,在XML文件中没有
productCode
,而在JSON中则有。我不知道XML中的
productCode
有什么问题,因为我在这两种产品中都传递了
super(productCode)

这个代码怎么了

public static void xmlConverter(List<Product> products) {
    XStream xstream = new XStream();

    File xmlOutput = new File("data/Products.xml");

    PrintWriter xmlPrintWriter = null;
    try {

        xmlPrintWriter = new PrintWriter(xmlOutput);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    xmlPrintWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> \n <products>");

    xstream.alias("product", Product.class);        
    xstream.alias("refreshment", Refreshment.class);
    xstream.alias("parkingpass", ParkingPass.class);

    for (Product aProduct: products) {
        String productOutput = xstream.toXML(aProduct);
        xmlPrintWriter.write( "\n" + productOutput);
    } 

    xmlPrintWriter.write("\n");
    xmlPrintWriter.write("</products>");

    xmlPrintWriter.close();
}
Product.java

package Products;

public abstract class Product {

    private String productCode;

    public Product(String productCode) {
        super();
        this.productCode = productCode;
    }

    public String getProductCode() {
        return this.productCode;

    }

    public void setProductCode(String productCode) {
        this.productCode = productCode;

    }

    public abstract String getType();

}
ParkingPass.java

package Products;

public class ParkingPass extends Product {

    private double cost;

    public ParkingPass(String productCode, double cost) {
        super(productCode);
        this.cost = cost;
    }

    public double getParkingFee() {
        return cost;
    }

    public void setParkingFee(double cost) {
        this.cost = cost;
    }

    @Override
    public String getType() {
        return "P";

    }

}
DataConverter.java

import java.io.*;
import java.util.*;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.thoughtworks.xstream.XStream;

import Products.ParkingPass;
import Products.Product;
import Products.Refreshment;

public class DataConverter {

    public static List<Product> readProduct() {
        List<Product> productList = new ArrayList<Product>();

        Scanner scnr = null;

        try {
            scnr = new Scanner(new File("data/Products.dat"));
            scnr.nextLine();

            Product p = null;

            while (scnr.hasNext()) {
                String line = scnr.nextLine();
                String data[] = line.split(";");

                String productCode = data[0];
                String productType = data[1];

                String name = null;
                double cost = 0.0;
                double parkingFee = 0.0;


                if (data.length == 4) {
                    name = data[2];
                    cost = Double.parseDouble(data[3]);

                } else if (data.length == 3) {
                    parkingFee = Double.parseDouble(data[2]);
                }

                if (productType.equals("R")) {
                    p = new Refreshment(productCode, name, cost);
                } else if (productType.equals("P")) {
                    p = new ParkingPass(productCode, parkingFee);
                }

                productList.add(p);
            }
            scnr.close();
            return productList;


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void jsonConverter(List<Product> products) {

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        File jsonOutput = new File("data/Products.json");

        PrintWriter jsonPrintWriter = null;

        try {
            jsonPrintWriter = new PrintWriter(jsonOutput);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        for (Product aProduct : products) {
            String productOutput = gson.toJson(aProduct);
            jsonPrintWriter.write(productOutput + "\n");

        }
        jsonPrintWriter.close();
    }


    public static void xmlConverter(List<Product> products) {
        XStream xstream = new XStream();

        File xmlOutput = new File("data/Products.xml");

        PrintWriter xmlPrintWriter = null;
        try {

            xmlPrintWriter = new PrintWriter(xmlOutput);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        xmlPrintWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> \n <products>");

        xstream.alias("product", Product.class);        
        xstream.alias("refreshment", Refreshment.class);
        xstream.alias("parkingpass", ParkingPass.class);

        for (Product aProduct: products) {
            String productOutput = xstream.toXML(aProduct);
            xmlPrintWriter.write( "\n" + productOutput);
        } 

        xmlPrintWriter.write("\n");
        xmlPrintWriter.write("</products>");

        xmlPrintWriter.close();
    }

    public static void main(String[] args) {

        List<Product> productList = readProduct();

        jsonConverter(productList);
        xmlConverter(productList);

    }
}
}
import java.io.*;
导入java.util.*;
导入com.google.gson.gson;
导入com.google.gson.GsonBuilder;
导入com.thoughtworks.xstream.xstream;
进口产品.ParkingPass;
进口产品;
进口产品.点心;
公共类数据转换器{
公共静态列表readProduct(){
List productList=new ArrayList();
扫描仪scnr=空;
试一试{
scnr=新扫描仪(新文件(“data/Products.dat”);
序列号nextLine();
乘积p=null;
while(scnr.hasNext()){
String line=scnr.nextLine();
字符串数据[]=行。拆分(“;”);
字符串productCode=数据[0];
字符串productType=data[1];
字符串名称=null;
双倍成本=0.0;
双倍停车费=0.0;
如果(data.length==4){
名称=数据[2];
成本=Double.parseDouble(数据[3]);
}else if(data.length==3){
parkingFee=Double.parseDouble(数据[2]);
}
if(productType.equals(“R”)){
p=新点心(产品代码、名称、成本);
}else if(productType.equals(“P”)){
p=新停车通行证(产品代码,停车费);
}
产品列表。添加(p);
}
scnr.close();
返回产品列表;
}catch(filenotfounde异常){
e、 printStackTrace();
}
返回null;
}
公共静态无效jsonConverter(列出产品){
Gson Gson=new GsonBuilder().setPrettyPrinting().create();
File jsonOutput=new文件(“data/Products.json”);
PrintWriter jsonPrintWriter=null;
试一试{
jsonPrintWriter=新的PrintWriter(jsonOutput);
}catch(filenotfounde异常){
e、 printStackTrace();
}
对于(产品A产品:产品){
字符串productOutput=gson.toJson(一个产品);
jsonPrintWriter.write(productOutput+“\n”);
}
jsonPrintWriter.close();
}
公共静态转换器(列出产品){
XStream XStream=新的XStream();
文件xmlOutput=新文件(“data/Products.xml”);
PrintWriter xmlPrintWriter=null;
试一试{
xmlPrintWriter=新的PrintWriter(xmlOutput);
}catch(filenotfounde异常){
e、 printStackTrace();
}
xmlPrintWriter.write(“\n”);
别名(“产品”,product.class);
别名(“refresh”,refresh.class);
别名(“parkingpass”,parkingpass.class);
对于(产品A产品:产品){
字符串productOutput=xstream.toXML(一个产品);
xmlPrintWriter.write(“\n”+产品输出);
} 
xmlPrintWriter.write(“\n”);
xmlPrintWriter.write(“”);
xmlPrintWriter.close();
}
公共静态void main(字符串[]args){
List productList=readProduct();
jsonConverter(产品列表);
xmlConverter(产品列表);
}
}
}

import java.io.*;
import java.util.*;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.thoughtworks.xstream.XStream;

import Products.ParkingPass;
import Products.Product;
import Products.Refreshment;

public class DataConverter {

    public static List<Product> readProduct() {
        List<Product> productList = new ArrayList<Product>();

        Scanner scnr = null;

        try {
            scnr = new Scanner(new File("data/Products.dat"));
            scnr.nextLine();

            Product p = null;

            while (scnr.hasNext()) {
                String line = scnr.nextLine();
                String data[] = line.split(";");

                String productCode = data[0];
                String productType = data[1];

                String name = null;
                double cost = 0.0;
                double parkingFee = 0.0;


                if (data.length == 4) {
                    name = data[2];
                    cost = Double.parseDouble(data[3]);

                } else if (data.length == 3) {
                    parkingFee = Double.parseDouble(data[2]);
                }

                if (productType.equals("R")) {
                    p = new Refreshment(productCode, name, cost);
                } else if (productType.equals("P")) {
                    p = new ParkingPass(productCode, parkingFee);
                }

                productList.add(p);
            }
            scnr.close();
            return productList;


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void jsonConverter(List<Product> products) {

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        File jsonOutput = new File("data/Products.json");

        PrintWriter jsonPrintWriter = null;

        try {
            jsonPrintWriter = new PrintWriter(jsonOutput);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        for (Product aProduct : products) {
            String productOutput = gson.toJson(aProduct);
            jsonPrintWriter.write(productOutput + "\n");

        }
        jsonPrintWriter.close();
    }


    public static void xmlConverter(List<Product> products) {
        XStream xstream = new XStream();

        File xmlOutput = new File("data/Products.xml");

        PrintWriter xmlPrintWriter = null;
        try {

            xmlPrintWriter = new PrintWriter(xmlOutput);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        xmlPrintWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> \n <products>");

        xstream.alias("product", Product.class);        
        xstream.alias("refreshment", Refreshment.class);
        xstream.alias("parkingpass", ParkingPass.class);

        for (Product aProduct: products) {
            String productOutput = xstream.toXML(aProduct);
            xmlPrintWriter.write( "\n" + productOutput);
        } 

        xmlPrintWriter.write("\n");
        xmlPrintWriter.write("</products>");

        xmlPrintWriter.close();
    }

    public static void main(String[] args) {

        List<Product> productList = readProduct();

        jsonConverter(productList);
        xmlConverter(productList);

    }
}
}