工作中对xml的操作比较多,这里实现一种。使用dom4j解析xml、
流程:
创建xml----->然后解析--------》然后将数据用Log打印出来。
创建xml:
package com.example.jsonandxml; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import android.os.Environment; /** * <p> * 创建路径:/mn/sdcard/XmlParserDemo/master.xml * </p> * 下午11:55:27 * * @auther dalvikCoder */ public class CreateXmlAndInsertData { private static String ROOT_PATH = Environment.getExternalStorageDirectory() .getPath() + File.separator + "XmlParserDemo";// ----->/storage/emulated/0XmlParserDemo private static String XML_NAME = "master.xml"; /** * <p> * 创建xml文件 * * </p> * void */ public static void createXml() { Document doc = DocumentHelper.createDocument(); Element person = doc.addElement("person"); Element name = person.addElement("name"); Element firstName = name.addElement("firstname"); Element secondName = name.addElement("secondName"); Element address = person.addElement("address"); Element schoolAddress = address.addElement("schooladdress"); Element homeAddress = address.addElement("homeaddress"); Element grade = person.addElement("小学"); grade.addAttribute("name","初中"); // ---------增加结点,在赋值 name.addAttribute("id","001"); address.addAttribute("id","002"); firstName.setText("张三"); secondName.setText("李四"); schoolAddress.setText("中国北京"); homeAddress.setText("中国上海"); grade.setText("小学一年级"); OutputFormat format = OutputFormat.createPrettyPrint(); // 设置输出编码 format.setEncoding("UTF-8"); // 创建需要写入的File对象 File file = new File(ROOT_PATH + File.separator + XML_NAME);// ----》/storage/emulated/0/XmlParserDemo/master.xml // /storage/emulated/0/XmlParserDemo/master.xml if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } // 生成XMLWriter对象,构造函数中的参数为需要输出的文件流和格式 XMLWriter writer; try { writer = new XMLWriter(new FileOutputStream(file),format); // 开始写入,write方法中包含上面创建的Document对象 writer.write(doc); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
解析xml:
package com.example.jsonandxml; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import android.util.Log; /** * <p> * 目的:读取资源文件中的xml文件并解析,然后将数据插入数据库。 * * </p> * 下午11:42:12 * * @auther dalvikCoder */ public class XmlJieXi { public static HashMap<String,Document> xmlMap = new HashMap<String,Document>(); /** * <p> * 解析xml返回Document对象 * </p> * * @param fileName * @param bol * 是否直接返回缓存内的对象 * @return Document */ private static Document parse(String fileName,boolean bol) { if (fileName == null || fileName.equals("")) { return null; } Document doc = null; if (xmlMap.containsKey(fileName)) { if (bol) { return xmlMap.get(fileName); } else { doc = xmlMap.get(fileName); if (doc != null) { doc.clearContent(); xmlMap.remove(doc); doc = null; } return parse(fileName,false); } } else { try { InputStream in = new FileInputStream(fileName); SAXReader saxReader = new SAXReader(); doc = saxReader.read(in); in.close(); saxReader = null; in = null; xmlMap.put(fileName,doc); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return doc; } /** * <p> * 将xml中的数据插入数据库中 * </p> * * @param fileName * void */ public static void loadXmlAndInsertDB(String fileName) { if (!isFileExist(fileName)) { return; } Document doc = parse(fileName,false); if (doc == null) { return; } Element elem = doc.getRootElement(); @SuppressWarnings("unchecked") Iterator<Element> iterator = elem.elementIterator(); Element e = null; while (iterator.hasNext()) { e = iterator.next(); getDBDataModelOne(e); } } /** * <p> * 已知属性名称以及元素名称 * </p> * * @param e * void */ private static void getDBDataModelOne(Element e) { String name = e.getName(); if (name.equals("name")) { Iterator<Element> it = e.elementIterator(); while (it.hasNext()) { Element ele = it.next(); String value = ele.getText(); Log.e("0------------>",ele.getName() + "----" + value); } // 已知属性名称以及元素名称的情况下 String id = e.attributeValue("id"); // 处理插入数据库,这里就不操作数据库了,直接Log打印出来吧。 Log.e("-------------->",id); } else if (name.equals("address")) { // String scholladdr = e.getText(); } } /** * <p> * 判断文件是否存在 * </p> * @param fileName * @return boolean */ public static boolean isFileExist(String fileName) { if (fileName == null || fileName.equals("")) { return false; } File file = new File(fileName); return file.exists(); } /** * <p> * 释放资源 * </p> void */ public void releaseAll() { if (!xmlMap.isEmpty()) { xmlMap.clear(); } } /** * <p> * 释放单个资源 * </p> * @param fileName void */ public void releasegSingle(String fileName) { if (!xmlMap.isEmpty() && xmlMap.get(fileName) != null) { Document doc = xmlMap.get(fileName); doc.clearContent(); xmlMap.remove(doc); doc = null; } } }
这里四按钮对应的事件:
代码没有全部贴上来,本来想做的细点,由于时间关系以及最近身体不是很给力,不愿熬夜了,头疼,所以拖了很久才写这个博客。
@Override public void onClick(View v) { if (v == btnCreateXml) { CreateXmlAndInsertData.createXml(); } else if (v == btnExportXml) { XmlJieXi.loadXmlAndInsertDB(Environment .getExternalStorageDirectory() + File.separator + "XmlParserDemo" + File.separator + "master.xml"); } else if (v == btnParseXml) { // 创建路径:/mn/sdcard/XmlParserDemo/master.xml XmlJieXi.loadXmlAndInsertDB(Environment .getExternalStorageDirectory() + File.separator + "XmlParserDemo" + File.separator + "master.xml"); } }
贴上图:
对xml,excel的处理等好多情况下,都会用到,所以这里简单总结下,以后或许会用到,就不重复造轮子了。