gpt4 book ai didi

android - 在 Android 中读取/写入外部 XML 文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:43 24 4
gpt4 key购买 nike

我想了解更多关于在 Android 中读取/写入 XML 文件(作为一种数据库)的信息。我似乎找不到任何相关信息,所以我想我不知道要寻找什么术语。

我的目标是将来自两个 editText 字段的用户名和密码写入文件,然后在稍后为我的应用程序创建登录功能时读取它们(并希望成功验证它们)。

我希望读/写的文件位于服务器上,所以这对我来说有点复杂。

如果有人能帮我找到有关读取/写入 XML 文件的教程,我将非常高兴。

谢谢。

最佳答案

这是写入 XML 文件的代码:

final String xmlFile = "userData";
String userNAme = "username";
String password = "password";
try {
FileOutputStream fos = new FileOutputStream("userData.xml");
FileOutputStream fileos= getApplicationContext().openFileOutput(xmlFile, Context.MODE_PRIVATE);
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
xmlSerializer.setOutput(writer);
xmlSerializer.startDocument("UTF-8", true);
xmlSerializer.startTag(null, "userData");
xmlSerializer.startTag(null, "userName");
xmlSerializer.text(username_String_Here);
xmlSerializer.endTag(null, "userName");
xmlSerializer.startTag(null,"password");
xmlSerializer.text(password_String);
xmlSerializer.endTag(null, "password");
xmlSerializer.endTag(null, "userData");
xmlSerializer.endDocument();
xmlSerializer.flush();
String dataWrite = writer.toString();
fileos.write(dataWrite.getBytes());
fileos.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

要从 XML 文件中读取数据,请执行以下操作:

final String xmlFile = "userData";
ArrayList<String> userData = new ArrayList<String>();
try {
fis = getApplicationContext().openFileInput(xmlFile);
isr = new InputStreamReader(fis);
inputBuffer = new char[fis.available()];
isr.read(inputBuffer);
data = new String(inputBuffer);
isr.close();
fis.close();
}
catch (FileNotFoundException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
XmlPullParserFactory factory = null;
try {
factory = XmlPullParserFactory.newInstance();
}
catch (XmlPullParserException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
factory.setNamespaceAware(true);
XmlPullParser xpp = null;
try {
xpp = factory.newPullParser();
}
catch (XmlPullParserException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
xpp.setInput(new StringReader(data));
}
catch (XmlPullParserException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int eventType = 0;
try {
eventType = xpp.getEventType();
}
catch (XmlPullParserException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (eventType != XmlPullParser.END_DOCUMENT){
if (eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
}
else if (eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag "+xpp.getName());
}
else if (eventType == XmlPullParser.END_TAG) {
System.out.println("End tag "+xpp.getName());
}
else if(eventType == XmlPullParser.TEXT) {
userData.add(xpp.getText());
}
try {
eventType = xpp.next();
}
catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String userName = userData.get(0);
String password = userData.get(1);

关于android - 在 Android 中读取/写入外部 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13630844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com