较直观展示布局文件中的元素及其名字

1、背景:
有些Android应用程序的布局文件的内容很多,层次结构不明显;并且布局文件本身的个数有时候也会比较多,这样就给找view组件带来不便。
2、问题:
如何从布局(.xml)文件中,提取出view组件的类名和它的id名字,并于控制台窗口显示出来?
3、分析:
布局文件中的元素的形式为以下两种情况

<LinearLayout
    android:id="@+id/linear_layout"
   ... />

<LinearLayout
    android:id="@+id/linear_layout"  
    ... >
    ...
</LinearLayout>

4、方法:
首先将布局文件中的内容转换成字符串。建立三个ArrayList用来存储view组件的类名、它的id名字、以及它的层级。遍历字符串,直至最后。每遍历到一个view组件时,将其类名、id名和层级记录到对应的ArrayList中。没有id名字的,对应listNameString中元素值为空(""),listInteger中元素值大于等于0,数字越大级别越低。
将上述三个ArrayList的,按照一定的规则合并到listStringRecorde。我是将id名字追加在类名的后面,形成新的字符串,然后按照层级数别给新的字符串前面添加空格,其中空格个数与层级数成正比

private ArrayList<String> listString;     //view组件的类名
private ArrayList<String> listNameString; //它的id名字
private ArrayList<Integer> listInteger;   //它的层级
private ArrayList<String> listStringRecorde;  //最终生成的字符串

5、代码:ShowXml.class

package com.csdn.codeisgood;

import java.io.*;
import java.util.*;

public class ShowXml {
	
	private String xmlAbsPath = "";       //xml布局文件绝对路径
	private String targetPath = "";       //输出txt文件绝对路径
	private ArrayList<String> listString;      //view组件的类名
	private ArrayList<String> listNameString;  //它的id名字
	private ArrayList<Integer> listInteger;    //它的层级
	private ArrayList<String> listStringRecorde;  //最终生成的字符串
	
	ShowXml(String xmlAbsPath,String targetPath){
		this.xmlAbsPath = xmlAbsPath;
		this.targetPath = targetPath;
	}
	
	public String getXmlAbsPath() {
		return this.xmlAbsPath;
	}
	
	public void setXmlAbsPath(String xmlAbsPath) {
		this.xmlAbsPath = xmlAbsPath;
	}
	
	public String getTargetPath() {
		return this.targetPath;
	}
	
	public void setTargetPath(String targetPath) {
		this.targetPath = targetPath;
	}
	/*
	* 读取xml文件,转换成字符串返回
	*/
	private String getXml() {
		if(xmlAbsPath.isEmpty()) {
			return null;
		}
		BufferedReader bufferedReader = null;
		StringBuilder  content        = new StringBuilder();
		try {
			File file                           = new File(xmlAbsPath);
			FileInputStream fileInputStream     = new FileInputStream(file);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
			bufferedReader                      = new BufferedReader(inputStreamReader);
			String line = "";
			while((line = bufferedReader.readLine()) != null ) {
				content.append(line);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(bufferedReader != null) {
				try {
					bufferedReader.close();
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
		return content.toString();
	}
	/*
	* 遍历字符串,直至最后。每遍历到一个view组件时,将其类名、id名和层级记录到对应的ArrayList中
	*/
	public void analizeXmlString() {
		String string = getXml();
		if(string.isEmpty()) {
			return;
		}
		
		Integer integer = 0;
		String  st      = "";
		String  stName  = "";
		int firstSpeace = 0;
		int endSpeace   = 0;
		
		String subString = "";
		int firstName    = 0;
		int endName      = 0;
				
		listString = new ArrayList<String>();
		listNameString = new ArrayList<String>();
		listInteger = new ArrayList<Integer>();
		
		int i = 0;
		while( i < string.length() - 1) {
			if(string.substring(i,i+2).equals("<?")) {
				i = string.indexOf("?>");
			}
			if(string.substring(i,i+1).equals("<") && !string.substring(i+1,i+2).equals("/")) {
				firstSpeace = string.indexOf(" ",i);
				endSpeace   = string.indexOf(">",i);
				if(firstSpeace == -1) {
					return;
				}
				st = string.substring(i+1,firstSpeace);
				
				subString = string.substring(i,endSpeace);
				firstName = subString.indexOf("@+id/");
				if(firstName != -1) {
					endName   = subString.indexOf("\"",firstName);
					stName    = subString.substring(firstName+5,endName); //"@+id/".length() = 5
				}else {
					stName = "";
				}
				
				listNameString.add(stName);
				listString.add(st);
				listInteger.add(integer);	
				
				if(!string.substring(endSpeace-1,endSpeace).equals("/")) {
					integer = integer + 1;
				}
				i = endSpeace;
			}else {
				if(string.substring(i,i+1).equals("<") && string.substring(i+1,i+2).equals("/")) {
					endSpeace   = string.indexOf(">",i);
					String same = string.substring(i+1+1,endSpeace);
					for(int k = listString.size()-1; k >= 0; k--) {
						if(listString.get(k).toString().equals(same)) {				
							integer = listInteger.get(k);
							break;
						}
					}
				}
			}
			i ++;
		}
	}
	
	public void showXmlStructure() {
		if(listString.size() != listNameString.size()) {
			return;
		}else{
			if(listString.size() != listInteger.size()) {
				return;
			}
		}
		if(listString.size() == 0){
		    return;
		}
		
		listStringRecorde = new ArrayList<String>();
		String stringRecorde ="";
		String headString = "";
		
		for(int i = 0; i < listString.size(); i++) {
			if(!listNameString.get(i).isEmpty()) {
				stringRecorde = listString.get(i) + " 's name is " + listNameString.get(i);
			}else {
				stringRecorde = listString.get(i);
			}
			int k = 0;
			while(k < listInteger.get(i)) {
				headString = headString + "    ";
				k++;
			}
			listStringRecorde.add(headString+stringRecorde);
			headString = "";
		}
		
		for(int i = 0; i < listString.size(); i++) {
			System.out.println(listStringRecorde.get(i));	
		}

	}
	
	public void saveXmlStructureToText() {
	    if(listStringRecorde.size() == 0){
	        return;
	    }
	    FileWriter fwriter = null;
	    try {
	        fwriter = new FileWriter(targetPath, true);
	        for(int i = 0; i < listStringRecorde.size(); i++) {
	        	fwriter.write(listStringRecorde.get(i)+"\n");
	        }
	    } catch (IOException ex) {
	        ex.printStackTrace();
	    } finally {
	        try {
	            fwriter.flush();
	            fwriter.close();
	        } catch (IOException ex) {
	            ex.printStackTrace();
	        }
	    }
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值