andoird通过反射将xml数据映射到java类里
2018-01-17 17:24
399 查看
主界面很简单 封装的帮助类传入需要反射的类名 以及xml中的其它参数 得到一个实例化的对象
接下来看看xmlutils
最后主要的逻辑 写在XMLParaserUtil里的:
接下来看看xmlutils
最后主要的逻辑 写在XMLParaserUtil里的:
package com.example.tempand; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import java.lang.reflect.Field; import java.util.HashMap; public class XMLParserUtil<T> extends DefaultHandler{ private StringBuilder text = new StringBuilder(); // 定义变量保存解析结果 public HashMap<String,T> usermap = null; // 定义XML的标签及标签属性名称 public String SAX_ROOT = "config"; public String SAX_LOTTERY = "item"; private final Field[] fields; private Class<T> data; private String index; XMLParserUtil(String index, Class<T> t, String nodename, String rootnode){ fields =t.getDeclaredFields();//通过反射获取类里面的变量 this.data=t; this.index=index; this.SAX_ROOT=rootnode; this.SAX_LOTTERY=nodename; } public HashMap<String,T> getUsermap(){ return usermap; } @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub super.startDocument(); } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub super.endDocument(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // TODO Auto-generated method stub super.startElement(uri, localName, qName, attributes); if (localName.equalsIgnoreCase(SAX_ROOT)) {// 根元素 if (null == usermap) { usermap = new HashMap<String, T>(); } else { usermap.clear(); } } else if (localName.equalsIgnoreCase(SAX_LOTTERY)) { try { T obj = data.newInstance(); for (int i = 0; i < fields.length; i++) { fields[i].getName(); String value = attributes.getValue(fields[i].getName()); fields[i].setAccessible(true); if (null!=value&&!"".equals(value)) fields[i].set(obj,value); //给变量付值 } usermap.put(attributes.getValue(index),obj); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } else { // Nothing to do ... } } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub super.characters(ch, start, length); text.setLength(0); text.append(String.valueOf(ch, start, length)); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub super.endElement(uri, localName, qName); if (localName.equalsIgnoreCase(SAX_ROOT)) {// 根元素 //nothing } // } }
相关文章推荐
- java读取xml获得list并通过反射赋值给javabean
- 通过JAVA写数据到XML里面
- 通过java程序读取文件xml中的数据
- java web 通过前台输入的数据(name-value)保存到后台 xml文件中
- Java 通过HttpURLConnection Post方式提交xml,并从服务端返回数据
- JAVA调用返回XML格式数据的WebService,并通过XPath解析XML的应用
- Java反射xml数据到类中
- java通过反射机制为实例添加数据
- 通过映射关系 动态转义为统一格式的数据 (支持 JSON 和 XML )
- JAX-RPC 1.1中的Java/XML数据类型映射
- 通过反射转换java数据类型
- ORM框架通过映射(反射)获取数据库的数据
- Java 通过HttpURLConnection Post方式提交xml,并从服务端返回数据
- java中通过xsl将xml数据装换为html格式字符串
- java通过反射和JDOM解析xml成对象
- Java反射实现XML数据拷贝到Bean
- java通过反射获取私有的构造方法,及反射擦除泛型数据类型约束
- java通过url获取页面数据 java解析xml 基金净值接口使用方法
- java通过反射Method、属性字段名、值、数据类型
- Java 通过httpClient Post方式提交xml,并从服务端返回数据