简体   繁体   English

编写我自己的Android序列化类

[英]Writing my own Android Serialization class

I am writing my own android serialization class to convert an object to xml. 我正在编写自己的android序列化类,以将对象转换为xml。 I have created a Bar class that consists of the colour,Rect and Division of a bar. 我创建了一个Bar类,该类由颜色,Rect和Bar的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;
    }

}

In my main activity I create two bars and add them to an array. 在我的主要活动中,我创建了两个条形并将它们添加到数组中。 I want to loop through the array and get the colour of each bar and write this to an xml file. 我想遍历数组并获取每个条形的颜色并将其写入xml文件。 However once the file is created the only thing that is written to the xml file is my xml header.Below is my code: 但是,一旦创建了文件,写入xml文件的唯一内容就是我的xml标头,下面是我的代码:

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

    }
}

Can anyone help me and tell me where I am going wrong? 谁能帮助我,告诉我我要去哪里了?

The simplest way to write out XML data is using XmlSerializer in the SDK ( docs link ). 写出XML数据的最简单方法是使用SDK中的XmlSerializerdocs链接 )。 This keeps you from needing to provide all the boilerplate like headers and all the bracket syntax, you just focus on starting and ending the tags and adding the data elements and attributes in between. 这使您无需提供所有样板文件(如标头)和所有方括号语法,您只需专注于标签的开始和结束以及在两者之间添加数据元素和属性。

I'd rather use JSON. 我宁愿使用JSON。

Serializing: 序列化:

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

Deserializing: 反序列化:

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

By the way: Try to stick to Camel Case notation: ThisIsAClassName , and thisIsAnObjectName . 顺便说一句:尝试坚持驼峰式的ThisIsAClassName方式: ThisIsAClassNamethisIsAnObjectName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM