简体   繁体   中英

Writing my own Android Serialization class

I am writing my own android serialization class to convert an object to xml. I have created a Bar class that consists of the colour,Rect and Division of a bar.

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. However once the file is created the only thing that is written to the xml file is my xml header.Below is my code:

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 ). 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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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