利用Dom4j 解析XML文件

@Test
public void testRead() throws Exception{
//创建SAXReader对象
SAXReader reader = new SAXReader();
//获取根节点元素对象
InputStream ism = getClass().getClassLoader().getResourceAsStream(“src/test/java/com/dom4j/studentnew.xml”);
if(ism == null){
throw new TemcException(“未查询到报告模板文件,请确认!”);
}
//读取文件 转换成Document
Document document = reader.read(ism);
//获取根节点元素对象
Element root = document.getRootElement();//根节点
List listRoot = root.attributes();//TODO 根阶段属性,需要时在增加
//迭代当前节点下面的所有子节点
for (Iterator fathers = root.elementIterator(); fathers.hasNext(); ) {
Element father = (Element) fathers.next();
System.out.println(“当前节点的标签:” + father.getName());
List propertyList = father.attributes();//节点属性集合
if(StringUtils.equals(“chapter”,father.getName())){//文章的章标签chapter
List listObject = Lists.newArrayList();
ReportChapter reportChapter = new ReportChapter();
//遍历 子节点
getChildIterator(father,reportChapter,father.getName());
}
}
}

/**
* 循环遍历Element元素的子集并赋值到Object
* @param father 当前级别Element
* @param o 当前级别对象
* @param parentType 父级类型(标签)
*/
public static void getChildIterator(Element father,Object o,String parentType){
for (Iterator childs = father.elementIterator();childs.hasNext();) {
Element child = (Element) childs.next();//下级节点
List propertyList = child.attributes();//下级节点属性
if(o==null) o=new Object();
if(StringUtils.equals(“section”, child.getName())){//文章下的字节
setReportSectionToReportChapter(o, propertyList,child);
}
}
}

/**
* 对象保存ReportSection–>ReportChapter
*/
private static void setReportSectionToReportChapter(Object o, List propertyList,Element father) {
ReportChapter reportChapter = (ReportChapter) o;
List listReportGraph = reportChapter.getReportElements();
if(listReportGraph==null)listReportGraph = Lists.newArrayList();
ReportSection reportSection = (ReportSection) setObjectValue(new ReportSection(),propertyList);
String type= reportSection.getType();
if(!StringUtils.equals(“text”, type)){//有字节,继续遍历
getChildIterator(father,reportSection,”section”);
}
listReportGraph.add(reportSection);
reportChapter.setReportElements(listReportGraph);
}

/**
* 对象赋值
* @param obj 需要赋值的对象
* @param list 对象属性集合list
* @return
*/

public static Object setObjectValue(Object obj, List list) {
for(Attribute attribute : list){
obj = setObjectValue(attribute.getName(),attribute,obj);
}
return obj;
}

/**
* 通过反射方式对对象赋值
* @param fieldName 需要反射的对象参数
* @param attribute 对象属性Attribute
* @param o 对象
* @return
*/
public static Object setObjectValue(String fieldName, Attribute attribute,Object o) {
try {
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String setter = “set” + firstLetter + fieldName.substring(1);
Field[] fields=o.getClass().getDeclaredFields();
String param = null;
Class params = null;
for(int i=0;i< fields.length;i++){
String oName = fields[i].getName();
if(StringUtils.equals(oName, fieldName)){
param = fields[i].getType().getName();
params = fields[i].getType();
break;
}
}
String value = attribute.getValue();
if(StringUtils.equals(param,”java.lang.Integer”) || StringUtils.equals(param,”int”)){
Method mSetter = o.getClass().getMethod(setter, new Class[] { params });
mSetter.invoke(o, Integer.parseInt(value!=null?value:”0”));
}else if (StringUtils.equals(param,”java.lang.Boolean”) || StringUtils.equals(param,”boolean”)) {
Method mSetter = o.getClass().getMethod(setter, new Class[] { params });
mSetter.invoke(o, Boolean.valueOf(value));
}else if (StringUtils.equals(param,”java.lang.Double”) || StringUtils.equals(param,”double”)) {
double d = Double.valueOf(value!=null?value:”0”);
Method mSetter = o.getClass().getMethod(setter, new Class[] { params });
mSetter.invoke(o, d);
}else if (StringUtils.equals(param,”java.lang.Float”)||StringUtils.equals(param,”float”)) {
float f = Float.valueOf(value!=null?value:”0”);
Method mSetter = o.getClass().getMethod(setter, new Class[] { params });
mSetter.invoke(o, f);
}else if (StringUtils.equals(param,”java.lang.Long”)||StringUtils.equals(param,”long”)) {
long f = Long.valueOf(value!=null?value:”0”);
Method mSetter = o.getClass().getMethod(setter, new Class[] { params });
mSetter.invoke(o, f);
}else if(StringUtils.equals(param,”java.lang.String”)){
Method mSetter = o.getClass().getMethod(setter, new Class[] { params });
mSetter.invoke(o, attribute.getValue());
}
return o;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

实体类就不贴了

你可能感兴趣的:(随笔)