编写我自己的Android Serialization类

时间:2013-02-19 15:42:07

标签: java android xml serialization xml-serialization

我正在编写自己的android序列化类来将对象转换为xml。我创建了一个Bar类,它由一个条形的颜色,Rect和Division组成。

public class Bar {

    String colour;
    int Rect;
    int Division;

    public Bar(String colour, int Rect, int Division) {

        this.colour = colour;
        this.Rect = Rect;
        this.Division = Division;

    }

    public String getColour() {

        return colour;
    }

    public int getRect() {

        return Rect;
    }

    public int getDivision() {

        return Division;
    }

}

在我的主要活动中,我创建了两个条形并将它们添加到数组中。我想循环遍历数组并获取每个条的颜色并将其写入xml文件。但是一旦创建了文件,写入xml文件的唯一内容就是我的xml header.Below是我的代码:

public class MainActivity extends Activity {

    private String header = "[xmlDoc setVersion:@" + "\" 1.0 \"" + "]" + "\n";
    private String barModel = "<barModel>" + "\n";
    private String bars = "<bars>" + "\n";
    private String bar = "<bar>" + "\n";
    private String rect1 = "<rect>";
    private String rect2 = "</rect>" + "\n";
    private String divisions = "<divisions>";
    private String divisions2 = "</divisions>" + "\n";
    private String colorId = "<colorId>";
    private String colorId2 = "</colorId>" + "\n";
    ArrayList<Bar> barList;
    Bar David;
    Bar Perrine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addtoArray();
        saveModel();
        setContentView(R.layout.activity_main);

    }

    public void addtoArray() {

        List<Bar> barList = new ArrayList<Bar>();

        barList.add(new Bar("Blue", 33, 8898));
        barList.add(new Bar("Red", 6876, 65));

    }

    protected void saveModel() { // creat directory and file to write to File

        File xmlFile = new File(Environment.getExternalStorageDirectory()
                .getPath() + "/serializeObject");
        xmlFile.mkdirs();
        File file = new File(xmlFile, "personmodel.xml");

        try {

            FileWriter writer = new FileWriter(file);
            writer.append(header);
            writer.flush();

            // iterate through bars
            for (Bar array : barList) {

                String colour = array.getColour();
                writer.append(colorId);
                writer.append(colour);
                writer.append(colorId2);
                writer.flush();
                writer.close();

            }

        } catch (Exception e) {
            // Log.d("Downloader", e.getMessage());
        }

    }
}

任何人都可以帮助我并告诉我哪里出错了吗?

2 个答案:

答案 0 :(得分:0)

写出XML数据的最简单方法是在SDK中使用XmlSerializerdocs link)。这使您无需提供所有样板文件和所有括号语法,只需关注开始和结束标记以及在两者之间添加数据元素和属性。

答案 1 :(得分:0)

我宁愿使用JSON。

序列化:

try {
    JSONObject bar = new JSONObject();
    bar.put("color", yourBarObject.getColor());
    bar.put("rect", yourBarObject.getRect());
    bar.put("division", yourBarObject.getDivision());
    // Here you are
    String representation = bar.toString();

} catch (JSONException ex){
    ex.printStackTrace();
    throw new RuntimeException(ex);
}

反序列化:

try {
    JSONObject bar = new JSONObject(representation);
    String color = bar.getString("color");
    int division = bar.getInt("division");
    int rect = bar.getInt("rect");
    Bar bar = new Bar(color, rect, division);
} catch (JSONException ex){
    ex.printStackTrace();
    throw new RuntimeException(ex);
} 

顺便说一下:尽量坚持使用Camel Case表示法:ThisIsAClassNamethisIsAnObjectName