在Android中读/写外部XML文件

时间:2012-11-29 17:00:07

标签: android xml database

我正在尝试更多地了解如何在Android中读取/写入XML文件(作为一种数据库)。我似乎无法找到任何关于这一点,所以我想我不知道要寻找什么条件。

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

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

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

谢谢。

3 个答案:

答案 0 :(得分:14)

以下是写入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);

答案 1 :(得分:2)

要在 / storage / sdcard0 / your_app_name / 中创建新文件,请使用以下命令:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "your_app_name" + "/" + xmlFile);
        file.createNewFile();
        FileOutputStream fileos = new FileOutputStream(file);

答案 2 :(得分:-1)

公共类SkillsParser实现了AssyncXmlReader {

import Text.Read
import Control.Applicative

choose :: Maybe a -> Maybe b -> Maybe (Either a b)
choose x y = fmap Left x <|> fmap Right y

readListMaybe :: Read a => [String] -> Maybe [a]
readListMaybe = mapM readMaybe

toTuple :: [String] -> Maybe (Either [(Int, Int)] [(Char, Int)])
toTuple ss = readListMaybe ss `choose` readListMaybe ss

main = do
    -- Just (Left [(1,2),(1,3),(3,4),(1,4)])
    print $ toTuple ["(1,2)", "(1,3)" ,"(3,4)" ,"(1,4)"]
    -- Just (Right [('a',2),('b',3),('g',8),('h',2),('r',4)])
    print $ toTuple ["('a',2)", "('b',3)", "('g',8)", "('h',2)", "('r',4)"]

}