【发布时间】: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
请帮忙
【问题讨论】: