简体   繁体   English

在android上读写xml文件

[英]reading and writing to and from xml file on android

This is part of my string.xml file on an application that I'm trying to create: 这是我要创建的应用程序上的string.xml文件的一部分:

<array name="users">
    <item>
        <user_name>u1</user_name>
        <password>p1</password>
        <email>aaa@aaa</email>
    </item>
    <item>
        <user_name>u2</user_name>
        <password>p2</password>
        <email>bbb@bbb</email>
    </item>
    <item>
        <user_name>u3</user_name>
        <password>p3</password>
        <email>ccc@ccc</email>
    </item>
</array>

I have a problem reading the "users" two-dimensional array into a two-dimensional array on the java file. 我在将“用户”二维数组读取为java文件上的二维数组时遇到问题。 I've already created a class name "User" for the array. 我已经为数组创建了一个类名“ User”。 I'm quite stuck here. 我很困在这里。 Can anybody give me a hand? 有人可以帮我吗? Thanks 谢谢

strings.xml is not for what you are trying to do here. strings.xml不适用于您在此处尝试执行的操作。 It is supposed to have all strings that are displayed to the user to provide multi-language support. 它应该具有所有显示给用户的字符串以提供多语言支持。

If you want to store your app's domain data to XML file, you can create one in the assets folder and read it. 如果要将应用程序的域数据存储到XML文件,则可以在assets文件夹中创建一个并读取它。

It's ok. 没关系。 I've solved it. 我已经解决了 Here is my code: 这是我的代码:

String[] userArray = getResources().getStringArray(R.array.users);
User[] arr = new User[3];
for(int i=0 ; i<userArray.length ; i++)
arr[i] = new User(userArray[i]);

and this is my "User" class constructor: 这是我的“ User”类构造函数:

public User(String str)
{
    int num;
    str = str.substring(1);
    num = str.indexOf(' ');
    user_name = str.substring(0,num);
    String substr = str.substring(num+1);
    num = substr.indexOf(' ');
    password = substr.substring(0,num);
    email = substr.substring(num+1);
}

In that way I get an array of "User". 这样,我得到一个“用户”数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM