Android简单获取string.xml中名字和内容

来源:互联网 发布:子域名设置cname 编辑:程序博客网 时间:2024/06/10 23:39

我在开发的时候,需要把现有的项目进行国际化。

此时,需要把string.xml里面的内容抽取出来,放到表格中,发给翻译人员进行翻译。

搜索了好久网上的,没有符合的。

后来就自己写了个简单的demo。下面就是我写的简要代码。

public void readLine() throws Exception {    String s1 = "";    File file = new File("a.txt");    FileOutputStream in = new FileOutputStream("b.txt");    BufferedReader br = new BufferedReader(new FileReader(file));    String line = "";    String s3 = "\r\n";    while ((line = br.readLine()) != null) {        s1 = line;        if (s1.trim().length() == 0) {            continue;        }        String s2 = s1.substring(s1.indexOf("name=") + 6, s1.indexOf(">") - 1);        //如果xml的命名有前缀,可以方便的过滤        if (!s2.contains("")) {            continue;        }        in.write(s2.getBytes(), 0, s2.length());        in.write(s3.getBytes());    }    in.close();    br.close();}


public void readLine2() throws Exception {    String s1 = "";    File file = new File("a.txt");    OutputStreamWriter in = new OutputStreamWriter(new FileOutputStream("b.txt") ,"utf-8");    BufferedReader br = new BufferedReader(new FileReader(file));    String line = "";    String s3 = "\r\n";    while ((line = br.readLine()) != null) {        s1 = line;        if (s1.trim().length() == 0 || !s1.contains("/string")) {            continue;        }        String s2 = s1.substring(s1.indexOf("\">") + 2, s1.indexOf("/string") - 1);        in.write(s2);        in.write(s3);    }    in.close();    br.close();}

以下进行简单说明,我会先将内容拷贝到a文本。通过readline()方法,获取到名字。通过readline2()方法,获取到文本内容。并会将内容保存在b.txt文本中。由于只有中文会乱码,所以,只对输出的内容进行编码。这样就很方便的拿出了文本。

注: 1.未考虑到很多<array> <item> 等文本。

2.对应自己的业务进行修改代码即可。

3.不喜勿喷,纯属个人记录使用。有好的优化,可以给一下提醒。

原创粉丝点击