免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3299 | 回复: 2
打印 上一主题 下一主题

[Android] Android中提取xml中text字符串打印为string.xml格式 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-07-23 15:18 |只看该作者 |倒序浏览
如果想手工的将android中xml的的字符串全部提取出来是一件很头疼的事情,不尽耗时耗力还不能保证是否齐全。今天给各位带来自己原创的提取字符串的方法,希望能对各位有所帮助。
  1. public static void getFileInfo(File file) {
  2. BufferedReader br = null;
  3. String strXmlLine = "";
  4. System.out.println("<!--"+file.getName()+"-->");
  5. try {
  6. br = new BufferedReader(new FileReader(file));
  7. int lineNumber=0;// 第几行
  8. int postion=0;//按顺序出现的位置(用于命名)
  9. while ((strXmlLine = br.readLine()) != null) {
  10. lineNumber++;
  11. if(strXmlLine.contains("text=")&&!strXmlLine.contains("X")&&!strXmlLine.contains("\"\"")&&!strXmlLine.contains("@string"))
  12. {
  13. //   System.out.println("第"+lineNumber+"行:"+strXmlLine);
  14. postion++;
  15. System.out.println(" <string name=\""+file.getName().replace(".xml","")+"_"+postion+"\">"+strXmlLine.replace("android:text=","").replace("/>","").replace(">","").replace("\"","").trim()+"</string>");
  16. }
  17. }
  18. } catch (FileNotFoundException e) {
  19. // TODO Auto-generated catch block
  20. e.printStackTrace();
  21. } catch (IOException e) {
  22. // TODO Auto-generated catch block
  23. e.printStackTrace();
  24. }
  25. finally{
  26. try {
  27. br.close();
  28. } catch (IOException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. }
  32. }
  33. }
复制代码

论坛徽章:
39
白银圣斗士
日期:2015-11-24 10:40:40酉鸡
日期:2015-03-20 14:15:44寅虎
日期:2015-03-20 14:13:59午马
日期:2015-03-20 14:13:16白羊座
日期:2015-03-20 14:12:54金牛座
日期:2015-03-20 14:12:09双子座
日期:2015-03-20 14:11:57巨蟹座
日期:2015-03-20 14:11:44狮子座
日期:2015-03-20 14:11:29亥猪
日期:2015-03-20 14:16:24戌狗
日期:2015-03-20 14:16:40申猴
日期:2015-03-20 14:17:05
2 [报告]
发表于 2013-07-24 10:24 |只看该作者
只提取字符串?有啥用?
如果仅仅是提取字符串,一个正则表达式就搞定了

论坛徽章:
0
3 [报告]
发表于 2013-07-24 14:49 |只看该作者
:wink: 嗯,说的挺对。回复 2# rover12421


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP