JDom解决xml文件
1.解析Xml的类
/* * 文 件 名: SAXParseXmlFile.java * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved * 描 述: <描述> * 修 改 人: HAOHAO * 修改时间: 2012-9-4 * 跟踪单号: <跟踪单号> * 修改单号: <修改单号> * 修改内容: <修改内容> */package com.bb.parse;import java.io.File;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;/** * 通过JDom解析xml文件, 把解析的数据通过反射封装到实体Bean中 * <功能详细描述> * * @author HAOHAO * @version [版本号, 2012-9-4] * @see [相关类/方法] * @since [产品/模块版本] */public class SAXParseXmlFile{ private static final String FILE_PATH = "src/com/bb/parse/person.xml"; /** <一句话功能简述> * <功能详细描述> * @param args [参数说明] * * @return void [返回类型说明] * @exception throws [违例类型] [违例说明] * @see [类、类#方法、类#成员] */ public static void main(String[] args) { SAXParseXmlFile saxParse = new SAXParseXmlFile(); //解析xml文件 List<Person> person = saxParse.readXmlFile(FILE_PATH); for (Person person2 : person) { System.out.println(person2.toString()); } } /** * 解析Xml文件 * <功能详细描述> * * @return List<Person> [返回类型说明] * @exception throws [违例类型] [违例说明] * @see [类、类#方法、类#成员] */ @SuppressWarnings("unchecked") private List<Person> readXmlFile(String filePath) { SAXBuilder builder = new SAXBuilder(); List<Person> personList = new ArrayList<Person>(); try { //DOC文档 Document doc = builder.build(new File(filePath)); //获取根结点 Element rootElement = doc.getRootElement(); //获取子结点 List eleChild = rootElement.getChildren(); Person person; //遍历子节点 for (Iterator iterator = eleChild.iterator(); iterator.hasNext();) { Element node = (Element) iterator.next(); person = new Person(); //获取子结点的了结点 List nodeChild = node.getChildren(); //遍历孙子节点 for (Iterator iterator2 = nodeChild.iterator(); iterator2.hasNext();) { Element name = (Element) iterator2.next(); //获取已封装的实体数据Bean person = getPerson(person, name); } //存入List中 personList.add(person); } } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return personList; } /** * <一句话功能简述> * <功能详细描述> * @param person Person 需要封装的对象 * @param node Element 子节点 * * @return Person [返回类型说明] * @exception throws [违例类型] [违例说明] * @see [类、类#方法、类#成员] */ @SuppressWarnings("unchecked") private Person getPerson(Person person, Element node) { String nodeName = node.getName(); String methodName = "set" + nodeName.substring(0, 1).toUpperCase() + nodeName.substring(1); String nodeValue = node.getText(); //获取当前实例化的类名 Class clazz = Person.class; Method method; try { method = clazz.getMethod(methodName, String.class); method.invoke(person, nodeValue); } catch (SecurityException e1) { e1.printStackTrace(); } catch (NoSuchMethodException e1) { e1.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return person; }}
<?xml version="1.0" encoding='UTF-8'?><persons><person><name>张三</name><sex>男</sex><age>32</age><email>zhangsan@gmail.com</email></person><person><name>关欣</name><sex>女</sex><age>28</age><email>guanxin@gmail.com</email></person><person><name>张三民</name><sex>男</sex><age>38</age><email>zhangsanmin@gmail.com</email></person></persons>
/* * 文 件 名: Person.java * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved * 描 述: <描述> * 修 改 人: HAOHAO * 修改时间: 2012-9-2 * 跟踪单号: <跟踪单号> * 修改单号: <修改单号> * 修改内容: <修改内容> */package com.bb.parse;/** * <一句话功能简述> * <功能详细描述> * * @author HAOHAO * @version [版本号, 2012-9-2] * @see [相关类/方法] * @since [产品/模块版本] */public class Person{ private String name; private String sex; private String age; private String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String toString() { return "name = " + name + ",sex = " + sex + ",age = " + age + ",email = " + email; }}