Read/write to external XML file in Android

后端 未结 3 1832
栀梦
栀梦 2020-12-08 12:01

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 te

3条回答
  •  佛祖请我去吃肉
    2020-12-08 12:35

    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();
    }
    

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

    final String xmlFile = "userData";
    ArrayList userData = new ArrayList();
    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);
    

提交回复
热议问题