简体   繁体   English

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

[英]how to write data to xml file and save that xml in android application

i am new developer in android application.i would like to write the data into xml file.i mean i would like to change the content like elements,tags,data in xml file.i can able to read an xml file in that file, i would like to change or modify and save that xml file. i am new developer in android application.i would like to write the data into xml file.i mean i would like to change the content like elements,tags,data in xml file.i can able to read an xml file in that file,我想更改或修改并保存 xml 文件。

i have written sample application to read xml as follows我编写了示例应用程序来读取 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");

i have written this code in a method here book.xml is my xml file to change我已经在本书的方法中编写了此代码。xml是我要更改的 xml 文件

when i call this method then i am getting out put with tags in logcat as follows当我调用此方法时,我将在 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>

how can write or edit or modify the above xml as prophetically?如何像预言一样编写或编辑或修改上述 xml?

please any body can help请任何机构都可以提供帮助

thanks in advance.提前致谢。

You could write a parser for your xml (use SAX for example) and store it into a object of a class which represents the fields and values of you xml. You could write a parser for your xml (use SAX for example) and store it into a object of a class which represents the fields and values of you xml.

Then edit the needed settings in the object and use a xml builder to make a xml file with the new values.然后在 object 中编辑所需的设置,并使用 xml 构建器创建一个具有新值的 xml 文件。

Then just write the file back to the device.然后只需将文件写回设备即可。 See http://developer.android.com/guide/topics/data/data-storage.html for details on storing files.有关存储文件的详细信息,请参阅http://developer.android.com/guide/topics/data/data-storage.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM