作为一个屌丝程序员不得不收藏的工具类 一 XML工具类之dom4j
2014-10-29 11:42
701 查看
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.xml.sax.SAXException; public class XMLUtil { /** * 获取整个xml信息 * @param path * @param c 反射的类名 * @return * @throws DocumentException */ public List<HashMap<String,String>> loaderXml(String path,Class c) throws DocumentException{ Field[] fields = c.getDeclaredFields(); List<HashMap<String,String>> result=new ArrayList<HashMap<String,String>>(); SAXReader sr = new SAXReader();//获取读取xml的对象。 //得到xml所在位置。然后开始读取。并将数据放入doc中 Document doc = sr.read(path); Element el_root = doc.getRootElement();//向外取数据,获取xml的根节点。 Iterator it = el_root.elementIterator();//从根节点下依次遍历,获取根节点下所有子节点 int i=0; while(it.hasNext()){//遍历该子节点 Object o = it.next();//再获取该子节点下的子节点 Element el_row = (Element)o; String s = el_row.getText(); Iterator it_row = el_row.elementIterator(); while(it_row.hasNext()){//遍历节点 HashMap<String,String> map=new HashMap<String,String>(); for (Field field : fields) { Element el_ename = (Element)it_row.next();//获取该节点下的所有数据。 map.put(field.getName(), el_ename.getText()); } result.add(i,map); } i++; } return result; } /** * 获取整个xml信息 * @param path * @param nodenames 节点名 * @return * @throws DocumentException */ public List<HashMap<String,String>> loaderXml(String path,String...nodenames) throws DocumentException{ List<HashMap<String,String>> result=new ArrayList<HashMap<String,String>>(); SAXReader sr = new SAXReader();//获取读取xml的对象。 //得到xml所在位置。然后开始读取。并将数据放入doc中 Document doc = sr.read(path); Element el_root = doc.getRootElement();//向外取数据,获取xml的根节点。 Iterator it = el_root.elementIterator();//从根节点下依次遍历,获取根节点下所有子节点 int i=0; while(it.hasNext()){//遍历该子节点 Object o = it.next();//再获取该子节点下的子节点 Element el_row = (Element)o; String s = el_row.getText(); Iterator it_row = el_row.elementIterator(); while(it_row.hasNext()){//遍历节点 HashMap<String,String> map=new HashMap<String,String>(); for (String str : nodenames) { Element el_ename = (Element)it_row.next();//获取该节点下的所有数据。 map.put(str, el_ename.getText()); } result.add(i,map); } i++; } return result; } /** * 写入xml * @param <T> * @param path 路径 * @param lists 写入的集合 * @throws IOException * @throws SAXException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InvocationTargetException * @throws SecurityException * @throws NoSuchMethodException */ public <T> void writeXML(String path,List<T> lists) throws IOException, SAXException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException{ T c=lists.get(0); Field[] fields = c.getClass().getDeclaredFields(); Document doc=DocumentHelper.createDocument(); doc.setXMLEncoding("UTF-8"); Element root = doc.addElement(c.getClass().getName()+"s"); for (T list : lists) { Element e = root.addElement(c.getClass().getName()); for (Field field : fields) { String fieldname=field.getName(); Method method = c.getClass().getMethod("get"+fieldname.substring(0,1).toUpperCase()+fieldname.substring(1)); e.addElement(fieldname).addText(method.invoke(list).toString()); } } OutputFormat format = OutputFormat.createPrettyPrint(); //设置XML文档输出格式 format.setEncoding("UTF-8"); //设置XML文档的编码类型 XMLWriter xmlWriter=new XMLWriter(new FileOutputStream(new File(path)),format); xmlWriter.write(doc); xmlWriter.close(); } /** * 先读再追加 * @param <T> * @param path 路径 * @param lists 追加的集合 * @throws IOException * @throws SAXException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InvocationTargetException * @throws SecurityException * @throws NoSuchMethodException * @throws DocumentException */ public <T> void additionalXML(String path,List<T> lists) throws IOException, SAXException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, DocumentException{ T c=lists.get(0); Field[] fields = c.getClass().getDeclaredFields(); SAXReader read=new SAXReader(); Document doc=read.read(new FileInputStream(path)); doc.setXMLEncoding("UTF-8"); Element root = doc.getRootElement(); for (T list : lists) { Element e = root.addElement(c.getClass().getName()); for (Field field : fields) { String fieldname=field.getName(); Method method = c.getClass().getMethod("get"+fieldname.substring(0,1).toUpperCase()+fieldname.substring(1)); e.addElement(fieldname).addText(method.invoke(list).toString()); } } OutputFormat format = OutputFormat.createPrettyPrint(); //设置XML文档输出格式 format.setEncoding("UTF-8"); //设置XML文档的编码类型 XMLWriter xmlWriter=new XMLWriter(new FileOutputStream(new File(path)),format); xmlWriter.write(doc); xmlWriter.close(); } //测试 public static void main(String[] args) throws IOException, SAXException { List<Student> lists=new ArrayList<Student>(); lists.add(new Student("0011", "za", "za@163.com", 50, 19, true)); lists.add(new Student("0021", "zb", "zb@163.com", 51, 20, true)); lists.add(new Student("0031", "zc", "zc@163.com", 52, 21, true)); lists.add(new Student("0041", "zd", "zd@163.com", 53, 22, true)); lists.add(new Student("0051", "ze", "ze@163.com", 54, 23, true)); XMLUtil xu=new XMLUtil(); try { xu.additionalXML("src\\student.xml", lists); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }不同需求可以自己再写,供参考……
相关文章推荐
- 作为一个屌丝程序员不得不收藏的工具类 一 文件操作类
- 作为一个屌丝程序员不得不收藏的工具类 一 网站爬虫工具类
- 作为一个屌丝程序员不得不收藏的工具类 一 邮件验证类
- 作为一个屌丝程序员不得不收藏的工具类 一 基于spring的属性文件工具类
- 作为一个屌丝程序员不得不收藏的工具类 一 IP获取工具类
- 作为一个屌丝程序员不得不收藏的工具类 一 json工具类
- 对xml的处理_Dom4j入门_一个工具类
- XML 作为一个更好的网络数据表达方式(NDR)
- 作为一个合格程序员每天该做的事
- Free Talk : 转载:作为一个合格程序员每天该做的事
- 作为一个好的程序员,都需要具备哪些基本的素质和能力
- 作为一个合格程序员每天该做的事[作者:IT浪子]
- 最简单的一个XML解析-dom4j
- 别人做的一个XML+XSL的实例(值得初学者收藏)
- 作为一个好的程序员,都需要具备哪些基本的素质和能力
- 作为一个合格程序员每天该做的事
- 作为一个程序员的感想
- 作为一个JAVA程序员,我经常去的地方
- 作为一个合格程序员每天该做的事
- 作为一个合格程序员每天该做的事