android:查找布局文件中有id属性的内容,并生成java转换代码

1.java代码

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultAttribute;

/**
 * Java递归遍历XML所有元素
 * 
 * @author  Administrator
 * @version  [版本号, Apr 13, 2010]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class XmlParser
{
    // private static Map xmlmap = new HashMap(); 
    //存储xml元素信息的容器 
    private static ArrayList<Leaf> elemList = new ArrayList<Leaf>();
    
    public static void main(String args[])
        throws DocumentException
    {
        XmlParser test = new XmlParser();
        String path = "D:\\android_workspace\\packagemanager\\res\\layout\\browse_app_item.xml";
        // 读取XML文件
        SAXReader reader = new SAXReader();
        Document doc = reader.read(path);
        // 获取XML根元素
        Element root = doc.getRootElement();
        test.getElementList(root);
        String x = test.getListString(elemList);
        System.out.println("-----------解析结果------------\n" + x);
    }
    
    /** 
     * 获取节点所有属性值
     * <功能详细描述>
     * @param element
     * @return
     * @see [类、类#方法、类#成员]
     */
    public String getNoteAttribute(Element element)
    {
        String xattribute = "";
        DefaultAttribute e = null;
        List list = element.attributes();
        for (int i = 0; i < list.size(); i++)
        {
            e = (DefaultAttribute)list.get(i);
            //System.out.println("name = " + e.getName() + ", value = " + e.getText());
//            xattribute += " [name = " + e.getName() + ", value = " + e.getText() + "]";
            if(e.getName().equals("id") ) {
            	xattribute += " [name = " + e.getName() + ", value = " + e.getText() + "]";
            	xattribute = e.getText().replace("@+id/", "");
            }
        }
        return xattribute;
    }
    
    /** 
     * 递归遍历方法 
     * <功能详细描述>
     * @param element
     * @see [类、类#方法、类#成员]
     */
    public void getElementList(Element element)
    {
        List elements = element.elements();
        // 没有子元素 
        if (elements.isEmpty())
        {
        	String[] paths = element.getPath().split("/");
            String xpath = paths[paths.length-1];
            String value = element.getTextTrim();
            elemList.add(new Leaf(getNoteAttribute(element), xpath, value));
        }
        else
        {
            // 有子元素
            Iterator it = elements.iterator();
            while (it.hasNext())
            {
                Element elem = (Element)it.next();
                // 递归遍历 
                getElementList(elem);
            }
        }
    }
    
    public String getListString(List elemList)
    {
        StringBuffer sb = new StringBuffer();
        for (Iterator it = elemList.iterator(); it.hasNext();)
        {
            Leaf leaf = (Leaf)it.next();
//            sb.append("xpath: " + leaf.getXpath()).append(", value: ").append(leaf.getValue());
//            if (!"".equals(leaf.getXattribute()))
//            {
//                sb.append(", Attribute: ").append(leaf.getXattribute());
//            }
//            sb.append("\n");
            sb.append("("+leaf.getXpath()+")" + leaf.getXattribute() );
            sb.append(" = ("+leaf.getXpath()+")findViewById(R.id. "+ leaf.getXattribute()+")\n");
        }
        return sb.toString();
    }
}

/** 
* xml节点数据结构 
*/
class Leaf
{
    // 节点属性
    private String xattribute;
    
    // 节点PATH
    private String xpath;
    
    // 节点值
    private String value;
    
    public Leaf(String xattribute, String xpath, String value)
    {
        this.xattribute = xattribute;
        this.xpath = xpath;
        this.value = value;
    }
    
    public String getXpath()
    {
        return xpath;
    }
    
    public void setXpath(String xpath)
    {
        this.xpath = xpath;
    }
    
    public String getValue()
    {
        return value;
    }
    
    public void setValue(String value)
    {
        this.value = value;
    }
    
    public String getXattribute()
    {
        return xattribute;
    }
    
    public void setXattribute(String xattribute)
    {
        this.xattribute = xattribute;
    }
}

2.测试文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
     <ImageView android:id="@+id/imgApp" android:layout_width="wrap_content"  
        android:layout_height="match_parent" ></ImageView>  
    <RelativeLayout android:layout_width="match_parent"  android:layout_marginLeft="10dip"  
        android:layout_height="40dip">  
        <TextView android:id="@+id/tvLabel" android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:text="AppLable : "></TextView>  
        <TextView android:id="@+id/tvAppLabel" android:layout_width="wrap_content"  
            android:layout_toRightOf="@id/tvLabel" android:layout_height="wrap_content"  
            android:layout_marginLeft="3dip" android:text="Label" android:textColor="#FFD700"></TextView>  
        <TextView android:id="@+id/tvName" android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:layout_below="@id/tvLabel"   
            android:text="包名:"></TextView>  
        <TextView android:id="@+id/tvPkgName" android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:layout_below="@id/tvAppLabel"  
            android:layout_alignLeft="@id/tvAppLabel" android:textColor="#FFD700"></TextView>  
    </RelativeLayout>  

    <RelativeLayout android:id="@+id/rlasdfdsfdsfdsf" android:layout_width="match_parent"  android:layout_marginLeft="10dip"  
        android:layout_height="40dip">  
        <TextView android:id="@+id/tvLabel2" android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:text="AppLable : "></TextView>  
        <TextView android:id="@+id/tvAppLabel" android:layout_width="wrap_content"  
            android:layout_toRightOf="@id/tvLabel2" android:layout_height="wrap_content"  
            android:layout_marginLeft="3dip" android:text="Label" android:textColor="#FFD700"></TextView>  
        <TextView android:id="@+id/tvName2" android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:layout_below="@id/tvLabel2"   
            android:text="包名:"></TextView>  
        <TextView android:id="@+id/tvPkgName2"  android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:layout_below="@id/tvAppLabel"  
            android:layout_alignLeft="@id/tvAppLabel" android:textColor="#FFD700"></TextView>  
            <LinearLayout >
                <TextView android:id="@+id/xxxxxxx" android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:layout_below="@id/tvLabel2"   
            android:text="包名:"></TextView>  
            <LinearLayout>
                <com.xx.as.TextView android:id="@+id/ttsss" android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:layout_below="@id/tvLabel2"   
            android:text="包名:"></com.xx.as.TextView>  
                </LinearLayout>
            </LinearLayout>
    </RelativeLayout>  
    
</LinearLayout>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值