利用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;
}
}

实体类就不贴了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值