【问题标题】:how to write streamly xml file using serializer如何使用序列化程序编写流式 xml 文件
【发布时间】:2011-08-11 14:07:15
【问题描述】:

您好,我需要在 android 应用程序中编写添加经度纬度和心率的 xml 文件

当我这样使用它时

声明:public XmlSerializer serializer = Xml.newSerializer(); 初始化:

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

它不会抛出任何异常,只会创建一个 bald xml 文件。我不知道为什么,但是当在结束保存的函数中我写serializer.endTag(null, "root"); 它抛出异常数组越界。它的行为就像它没有流畅地保存数据。

我需要类似目标 C 中的 retain

请帮忙

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    我认为简单框架对你来说可能是个好主意: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>
    

    【讨论】:

    • 不完全是我做的,但还不错:) 我将在本周晚些时候将解决方案作为一个可选答案发布,但非常感谢大家
    【解决方案2】:

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

    【讨论】:

    • 感谢您的回答,但这对我不利,我不想创建一个列表然后保存它,我需要在获得数据后立即执行此操作
    • 你在哪里调用 AddNodes 方法?
    • 当我得到数据时,我启动线程收集数据并使用处理程序将其发送回活动。然后我调用 AddNodes();很喜欢
    【解决方案3】:

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

    【讨论】:

    • 感谢您的回答,但我不能接受。我需要这样做,但如果我这样做,我认为在单独的线程中它可以工作
    猜你喜欢
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多