使用XmlPullParser制作BindView工具

2024-06-24 10:18

本文主要是介绍使用XmlPullParser制作BindView工具,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在之前我写过了一个BindView的工具,之前使用的最要是正则表达的文本分析做的。最近,工作我认识了Android的XML解析,我又想起了这个问题。发现这个问题,其实用XmlPullParser更好解决。所以我重新写了这个工具。简单多了,而且不用格式化代码。

先分析一下如何写,简易思路如下

Created with Raphaël 2.1.0 输入文本路径 读取xml标签 是否存在ID 生成一个findView 拷贝即可 yes no

代码如下:

package com.owant.example.java;import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;/*** Created by owant on 19/10/2016.* 查找XML布局下的控件,进行生产findViewById的代码*/public class BindViewTool {public static void main(String[] arg) {bindView("/Users/owant/AndroidStudioProjects/201610/owant2/src/main/res/layout/test_bind_view.xml");}/*** private TextView info;*/private static String declare_format = "private {0} {1};";/*** info=(TextView)findViewById(R.id.text);* info=(TextView)getView().findViewById(R.id.text);*/private static String find_view_format = "{0} = ({1}){2}findViewById({3});";private static boolean isFragment = false;/*** 忽略的标识*/private static String ignoreMark = "";//找到了需要绑定的Viewprivate static ArrayList<Model> bindViews;public static void bindView(String xmlPath) {try {InputStream inputStream = new FileInputStream(new File(xmlPath));//bindView的集合bindViews = new ArrayList<>();XmlPullParser xmlPullParser = XmlPullParserFactory.newInstance().newPullParser();xmlPullParser.setInput(inputStream, "utf-8");//xmlpullparser是以事件触发为设计的代码int eventType = xmlPullParser.getEventType();while (eventType != XmlPullParser.END_DOCUMENT) {//文档结束switch (eventType) {case XmlPullParser.START_DOCUMENT://文档开始break;case XmlPullParser.START_TAG://标签开始//对于这个情况需要进行com.owant.example.view.DivViewString type = xmlPullParser.getName();int pointExist = type.lastIndexOf(".");if (pointExist != -1) {//substring这end是到end之前的//String sub = new String("你好呀!");//System.out.println(sub.substring(0, 1));>>你type = type.substring(pointExist + 1, type.length());}String androidIdValue = null;int count = xmlPullParser.getAttributeCount();for (int i = 0; i < count; i++) {String androidIdTag = xmlPullParser.getAttributeName(i);if (androidIdTag.equals("android:id")) {String androidIdTagValue = xmlPullParser.getAttributeValue(i);if (androidIdTagValue.startsWith("@+id/")) {androidIdValue = androidIdTagValue.replace("@+id/", "");//TODO ignore}}}if (androidIdValue != null) {Model model = new Model();model.mId = androidIdValue;model.mType = type;bindViews.add(model);}break;case XmlPullParser.END_TAG://标签结束break;}eventType = xmlPullParser.next();}//打印需要BindView的控件for (Model m : bindViews) {String declare = declare_format.replace("{0}", m.mType);declare = declare.replace("{1}", m.mId);System.out.println(declare);}System.out.println("\n\n");for (Model m : bindViews) {String find = find_view_format.replace("{0}", m.mId);find = find.replace("{1}", m.mType);if (isFragment) {find = find.replace("{2}", "getView().");} else {find = find.replace("{2}", "");}find = find.replace("{3}", "R.id." + m.mId);System.out.println(find);}} catch (XmlPullParserException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static class Model {public String mType;public String mId;}}

需要注意的有:

  • 对于com.view.DivView的情况;
  • 对于是否有ignore标记的情况;
  • 对于是否为fragment的情况

运行结果:
这里写图片描述

这篇关于使用XmlPullParser制作BindView工具的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1089869

相关文章

使用Python将PDF表格自动提取并写入Word文档表格

《使用Python将PDF表格自动提取并写入Word文档表格》在实际办公与数据处理场景中,PDF文件里的表格往往无法直接复制到Word中,本文将介绍如何使用Python从PDF文件中提取表格数据,并将... 目录引言1. 加载 PDF 文件并准备 Word 文档2. 提取 PDF 表格并创建 Word 表格

使用Python实现局域网远程监控电脑屏幕的方法

《使用Python实现局域网远程监控电脑屏幕的方法》文章介绍了两种使用Python在局域网内实现远程监控电脑屏幕的方法,方法一使用mss和socket,方法二使用PyAutoGUI和Flask,每种方... 目录方法一:使用mss和socket实现屏幕共享服务端(被监控端)客户端(监控端)方法二:使用PyA

Python使用Matplotlib和Seaborn绘制常用图表的技巧

《Python使用Matplotlib和Seaborn绘制常用图表的技巧》Python作为数据科学领域的明星语言,拥有强大且丰富的可视化库,其中最著名的莫过于Matplotlib和Seaborn,本篇... 目录1. 引言:数据可视化的力量2. 前置知识与环境准备2.1. 必备知识2.2. 安装所需库2.3

Python数据验证神器Pydantic库的使用和实践中的避坑指南

《Python数据验证神器Pydantic库的使用和实践中的避坑指南》Pydantic是一个用于数据验证和设置的库,可以显著简化API接口开发,文章通过一个实际案例,展示了Pydantic如何在生产环... 目录1️⃣ 崩溃时刻:当你的API接口又双叒崩了!2️⃣ 神兵天降:3行代码解决验证难题3️⃣ 深度

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时

Linux镜像文件制作方式

《Linux镜像文件制作方式》本文介绍了Linux镜像文件制作的过程,包括确定磁盘空间布局、制作空白镜像文件、分区与格式化、复制引导分区和其他分区... 目录1.确定磁盘空间布局2.制作空白镜像文件3.分区与格式化1) 分区2) 格式化4.复制引导分区5.复制其它分区1) 挂载2) 复制bootfs分区3)

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Springboot3 ResponseEntity 完全使用案例

《Springboot3ResponseEntity完全使用案例》ResponseEntity是SpringBoot中控制HTTP响应的核心工具——它能让你精准定义响应状态码、响应头、响应体,相比... 目录Spring Boot 3 ResponseEntity 完全使用教程前置准备1. 项目基础依赖(M