主题 : Android中提取xml中text字符串并打印为string.xml格式 复制链接 | 浏览器收藏 | 打印
欢迎加入清源的android开发交流群:314230976,加群时请验证:arm,谢谢!
级别: 侠客
UID: 94332
精华: 0
发帖: 72
金钱: 370 两
威望: 74 点
贡献值: 0 点
综合积分: 144 分
注册时间: 2013-07-14
最后登录: 2013-09-25
楼主  发表于: 2013-07-23 16:46

 Android中提取xml中text字符串并打印为string.xml格式

如果想手工的将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. }
欢迎加入android开发交流群,群号是:314230976