如何使用序列化程序编写流式xml文件

时间:2011-04-23 18:37:19

标签: java android xml

嗨,我需要在Android应用程序中编写xml文件,添加经度纬度和心率

当我像这样使用它时

声明: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");
        }

结束xml文件的过程:

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

添加节点的程序:

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

它不会抛出任何异常,只会创建一个光头xml文件。我不知道为什么但是当在结束保存的函数时我写serializer.endTag(null, "root");它会抛出异常数组超出范围。它的行为就像不能保存数据一样。

我需要客观C中的内容retain

请帮助

3 个答案:

答案 0 :(得分:2)

我认为Simple Framework对您来说可能是一个好主意:http://simple.sourceforge.net/home.php。您可以像这样创建类:

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

然后调用

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

您将获得一个xml文件:

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

答案 1 :(得分:1)

我没有完成您的完整代码,但您可以通过本教程获得一些指导http://www.ibm.com/developerworks/opensource/library/x-android/

答案 2 :(得分:1)

你的代码对于3个小属性来说似乎很复杂,你不觉得吗? 有一些更容易用android读取/写入XML的方法,也许this post会有所帮助。