我正在尝试了解有关在 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);
要在/storage/sdcard0/your_app_name/中创建新文件,请使用以下命令:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "your_app_name" + "/" + xmlFile);
file.createNewFile();
FileOutputStream fileos = new FileOutputStream(file);
public class SkillsParser implements AssyncXmlReader {
private String ns = "";
@Override
public Object parse(InputStream in) throws XmlPullParserException,
IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
return readSkills(parser);
} finally {
in.close();
}
}
private Object readSkills(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "skills");
List<ESkill> skills = new ArrayList<ESkill>();
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the entry tag
if (name.equals("skill")) {
skills.add(readSkill(parser));
} else {
skip(parser);
}
}
return skills;
}
private ESkill readSkill(XmlPullParser parser) throws XmlPullParserException, IOException {
/*
<skill>
<name>Software Engineering</name>
</skill>
*/
String skill_name = "";
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if ("name".equals(name)) {
skill_name = readName(parser);
} else {
skip(parser);
}
}
return new ESkill().setSkill(skill_name);
}
// Processes title tags in the feed.
private String readName(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "name");
String description = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "name");
return description;
}
// For the tags title and summary, extracts their text values.
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
}