读/写在Android的外部XML文件 [英] Read/write to external XML file in Android

查看:98
本文介绍了读/写在Android的外部XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更多地了解读取/写入到一个XML文件(作为一种数据库)的Andr​​oid系统。我似乎无法找到这事,所以我想我不知道要寻找什么条款。

I'm trying to learn more about reading/writing to an XML-file (as a kind of database) in Android. I can't seem to find anything about this so I guess I don't know what terms to look for.

我的目标是写两个EDITTEXT字段用户名和密码进入该文件,然后读取它们(并希望取得成功验证他们)后,当我准备做一个登录功能为我的应用程序上。

My goal is to write usernames and passwords from two editText-fields into the file, then read them (and hopefully succeed validating them) later on when I am going to make a login function for my app.

我想读/写文件所在的服务器上,因此这使得它有点复杂,我。

The file I wish to read/write to is located on a server, so this makes it a bit complex for me.

如果有人可以帮助我找到一个关于读/写XML的文件,我会很高兴的教程。

If someone could help me find a tutorial about reading/writing to XML-files I would be very happy.

感谢你。

推荐答案

下面是code写XML文件:

Here is the code to write to XML file:

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数据文件的操作如下:

and to read data from XML File the do as below:

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的外部XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆