开发者

how to write streamly xml file using serializer

hi i need to write xml file in android app which add longitude latitude and heart rate

when i use it like this

declare:public XmlSerializer serializer = Xml.newSerializer(); init:

try {
            //we set the FileOutputStream as output for the serializer, using UTF-8 encoding
                    serializer.setOutput(outputStream, "UTF-8");
                    //Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null)
                    serializer.startDocument(null, Boolean.valueOf(true));
                    //set indentation option
                    serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
                    //start a tag called "root"
                    serializer.startTag(null, "root");
                    Log.e("Exception","++xml++ created xml file header");
        } catch (Exception e) {
            Log.e("Exception","++xml++ error occurred while creating xml file");
        }

procedure to end xml file:

final Button buttonEnd = (Button) findViewById(R.id.buttonEnd);
        buttonEnd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //XmlSerializer serializer2 = Xml.newSerializer();  
                try{
                    serializer.setOutput(outputStream, "UTF-8");
                    //serializer.endTag(null, "root");
                    serializer.endDocument();
                    //write xml data into the FileOutputStream
                    serializer.flush();
                    serializer = null;
                    //finally we close the file stream
                    outputStream.close();
                    Log.d("HXMTEST", "+++++ end of creating xml");
                }catch (Exception e) {
                    Log.e("Exception","++xml++ error occurred while ending xml file");
                }
                if(mConnectThread != null){
                    mConnectThread.cancel();
                }

                textview3.setText("the monitoring has been cancelled");
            }
        });

procedure to add nodes:

public void AddNodes(String heartBeat, String latitude, String longitude){
        //serializer = Xml.newSerializer();
        try{
         serializer.setOutput(outputStream, "UTF-8");
         serializer.startTag(null, "hb");
         if(heartBeat != null){
                 serializer.text(heartBeat);
             }else{
                 serializer.text("-1");
         }
         serializer.endTag(null, "hb");
         serializer.startTag(null, "latitude");
         if(latitude != null){
                 serializer.text(latitude);
             }else{
                 serializer.te开发者_高级运维xt("-1");
         }
         serializer.endTag(null, "latitude");
         serializer.startTag(null, "longitude");
         if(longitude != null){
                 serializer.text(longitude);
             }else{
                 serializer.text("-1");
         }
         serializer.endTag(null, "longitude");

        } catch (Exception e) {
            Log.e("Exception","++xml++ error occurred while adding xml file " +e.toString());
        }       
    }

it does not throw any exception but create only a bald xml file. i do not know why but when at the function which ends the saving i write serializer.endTag(null, "root"); it throws exception array out of bounds. it behaves like it does not save streamly the data.

i need something like in objective C is retain

please help


I think Simple Framework may be a good idea for you: http://simple.sourceforge.net/home.php . You can create class like this one:

@Root
public class Example {
    @Element
    private int longitude;

    @Element
    private int latitude;

    @Element
    private int heartrate;

    @Attribute
    private int index;

    public Example() {
        super();
    }

    public Example(int longitude, int latitude, int heartrate, int index) {
        this.longitude = longitude;
        this.latitude = latitude;
        this.heartrate = heartrate;
        this.index = index;
    }

    public int gelLatitude() {
        return latitude;
    }

    public int getLongitude() {
        return longitude;
    }

    public int gelHeartrate() {
        return heartrate;
    }

    public int getId() {
        return index;
    }
}

And then after calling

Serializer serializer = new Persister();
Example example = new Example(1, 2, 3, 123);
File result = new File("example.xml");
serializer.write(example, result);

you'll get an xml file as:

<example index="123">
<latitude>1</latitude>
<longitude>2</longitude>
<heartrate>3</heartrate>
</example>


I have not gone through your complete code, but you can get some guidance through this tutorial http://www.ibm.com/developerworks/opensource/library/x-android/


Your code seems complicated for 3 little attributes, don't you think? There are some easier ways to read/write XML with android, maybe this post will helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜