如何将数据写入xml文件并将该xml保存在android应用程序中

时间:2011-06-14 07:24:52

标签: android xml

我是android application.i中的新开发人员想将数据写入xml file.i意味着我想更改内容,如元素,标签,xml file.i中的数据,能够读取xml文件中的该文件,我想更改或修改并保存该xml文件。

我已经编写了示例应用程序来读取xml,如下所示

FileInputStream fstream = new FileInputStream("/sdcard/book.xml");
  BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
  String strLine;
  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   {
  // Print the content on the console
  System.out.println ("Xml file content====>"+strLine);


  data1.append(strLine);
  data1.append('\n');


  System.out.println("--------->"+data1.append(strLine)+"\n");

我已在此方法中编写此代码 book.xml 是我要更改的xml文件

当我调用此方法时,我将在logcat中输出标签,如下所示

06-14 12:48:32.309: VERBOSE/(433): #######>>>>><?xml version="1.0" encoding="utf-8"?>
06-14 12:48:32.309: VERBOSE/(433): <Employee-Detail>
06-14 12:48:32.309: VERBOSE/(433): <Employee>
06-14 12:48:32.309: VERBOSE/(433): <Emp_Id>E-001</Emp_Id>
06-14 12:48:32.309: VERBOSE/(433): <Emp_Name>Vinod</Emp_Name>
06-14 12:48:32.309: VERBOSE/(433): <Emp_E-mail>Vinod1@yahoo.com</Emp_E-mail>
06-14 12:48:32.309: VERBOSE/(433): </Employee>
06-14 12:48:32.309: VERBOSE/(433): <Employee>
06-14 12:48:32.309: VERBOSE/(433): <Emp_Id>E-002</Emp_Id>
06-14 12:48:32.309: VERBOSE/(433): <Emp_Name>Amit</Emp_Name>
06-14 12:48:32.309: VERBOSE/(433): <Emp_E-mail>Amit2@gmail.com</Emp_E-mail>
06-14 12:48:32.309: VERBOSE/(433): </Employee>
06-14 12:48:32.309: VERBOSE/(433): <Employee>
06-14 12:48:32.309: VERBOSE/(433): <Emp_Id>E-003</Emp_Id>
06-14 12:48:32.309: VERBOSE/(433): <Emp_Name>Deepak</Emp_Name>
06-14 12:48:32.309: VERBOSE/(433): <Emp_E-mail>Deepak3@hotmail.com</Emp_E-mail>
06-14 12:48:32.309: VERBOSE/(433): </Employee>
06-14 12:48:32.309: VERBOSE/(433): </Employee-Detail>

如何预先编写或编辑或修改上述xml?

请任何机构都可以提供帮助

提前感谢。

1 个答案:

答案 0 :(得分:6)

您可以为xml编写解析器(例如使用SAX)并将其存储到类的对象中,该类表示xml的字段和值。

然后编辑对象中所需的设置,并使用xml构建器生成包含新值的xml文件。

然后将文件写回设备。有关存储文件的详细信息,请参阅http://developer.android.com/guide/topics/data/data-storage.html