Java 对象转xml (dom 4j)

package cn.amberdata.erms.domain.model;

import cn.amberdata.erms.persistence.dao.ITreeViewDao;
import cn.amberdata.erms.persistence.po.TreeViewPO;
import cn.amberdata.erms.persistence.util.context.ApplicationUtils;
import com.datamber.common.session.DomainObject;
import com.datamber.common.session.Session;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
* 树形视图配置对象
* <p/>
* 适用 :在管理区,整理做树形菜单分类显示数据
* 或者用户组织架构显示数据
*
* @author
* @see TreeView
* @since 2019/5/8
*/

@NoArgsConstructor
@Data
public class TreeView extends DomainObject {
/**
* 唯一标识id
*/

private String objectId;
/**
* 名称
*/

private String name;

/**
* 区域:整理区 管理区
*/

private String region;

/**
* 全宗
*/

private String fondsId;

/**
* 视图的类型 比如:da_record ,da_volume 等
*/

private String type;
/**
* 过滤条件 如: isFiled=true and da_year='2019'
*/
private List<FilterCondition> filterCondition;
/**
* 有序节点集合
*/
private List<ViewNode> viewNodes;

/**
* 视图对象的完整表达 xml
*/
private String XmlConfig;

/**
* 判断是否存在
*
* @return true 存在 false 不存在
*/
public boolean isExist() {
ITreeViewDao treeViewDao = ApplicationUtils.getApplicationContext().getBean(ITreeViewDao.class);
return treeViewDao.isExist(this.getSession(), this.getName(), this.getRegion());
}


public boolean save() {


if (null == this.getObjectId()) {
//创建
ITreeViewDao treeViewDao = ApplicationUtils.getApplicationContext().getBean(ITreeViewDao.class);
TreeViewPO treeViewPO = new TreeViewPO();
BeanUtils.copyProperties(this, treeViewPO);
return treeViewDao.create(this.getSession(), treeViewPO);
}

return true;
}


public static class Builder {
private String id = null;
private Session session = null;

public Builder setId(String id) {
this.id = id;
return this;
}


public Builder setSession(Session session) {
this.session = session;
return this;
}

public TreeView builder() {
return new TreeView(this);
}

}


public TreeView(Builder builder) {
setSession(builder.session);
this.objectId = builder.id;
if (null == this.getSession()) {
throw new IllegalArgumentException("session is null...");
}

ITreeViewDao treeViewDao = ApplicationUtils.getApplicationContext().getBean(ITreeViewDao.class);
TreeViewPO treeViewPO = treeViewDao.getByObjectId(this.getSession(), objectId);
if (null != treeViewPO) {
BeanUtils.copyProperties(treeViewPO, this);
}

}


//关注这个方法 即可
//将对象生成xml
public String objectToXml(){
org.dom4j.Document document = DocumentHelper.createDocument();
// 2、创建根节点rss
Element root = document.addElement("viewconfig");
// 3、向rss节点添加version属性
root.addAttribute("fondsId", this.getFondsId());
root.addAttribute("name", this.getName());
root.addAttribute("objectId", this.getObjectId());
root.addAttribute("region", this.getRegion());
root.addAttribute("type", this.getType());
// 4、生成子节点及子节点内容
Element channel = root.addElement("querys");
for (FilterCondition filterCondition1 : this.getFilterCondition()){
Element title = channel.addElement("query");
title.addAttribute("connector", filterCondition1.getConnector());
title.addAttribute("field", filterCondition1.getField());
title.addAttribute("fieldType", filterCondition1.getFieldType());
title.addAttribute("keyWords", filterCondition1.getKeyWords());
title.addAttribute("name", filterCondition1.getName());
title.addAttribute("operation", filterCondition1.getOperation());
}

//递归创建node 节点
int size=this.getViewNodes().size();
ViewNode vn=this.getViewNodes().get(0);
Element node = root.addElement("node");
node.addAttribute("attrName", vn.getField());
node.addAttribute("attrTitle", vn.getName());
if(size>1){
getChildNodes(node,1,this.getViewNodes());
}
return document.asXML();
}


/**
* 递归 生成子节点
* @param node
* @param i
* @param listNode
*/
private static void getChildNodes(Element node, int i,List<ViewNode> listNode) {
//size 3 i 0
if (i<listNode.size()){
ViewNode vn=listNode.get(i);
Element node1 = node.addElement("node");
node1.addAttribute("attrName", vn.getField());
node1.addAttribute("attrTitle", vn.getName());
i++;
getChildNodes(node1,i,listNode);
}
}


}






package cn.amberdata.erms.domain.model;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
* 过滤条件对象
*
* @author
* @see
* @since 2019/5/13
*/

@Data
@NoArgsConstructor
public class FilterCondition {

/**
* 名称
*/

private String name;

/**
* 字段
*/

private String field;

/**
* 字段类型 如int varchar boolean
*/

private String fieldType;

/**
* 操作符
*/

private String operation ;
/**
* 关键字
*/

private String keyWords;
/**
* 连接符
*/

private String connector;

@Override
public String toString() {
String key=null;

if (fieldType.equalsIgnoreCase("int")||fieldType.equalsIgnoreCase("boolean")){
key=keyWords;
}else if (fieldType.equalsIgnoreCase("varchar")){
key="'"+keyWords+"'";
}
//TODO 时间的先放一下
String str=field+operation+key+connector;
return str;
}

}






package cn.amberdata.erms.domain.model;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.dom4j.Element;

/**
* 视图节点
*
* @author
* @see
* @since 2019/5/8
*/
@Data
@NoArgsConstructor
public class ViewNode {
/**
* 唯一标识 id
*/
private String objectId;
/**
* 名称
*/
private String name;
/**
* 对应的字段
*/
private String field;

/**
* 根据xml 元素构造对象
*
* @param element 元素
*/
public ViewNode(Element element) {
this.name = element.attribute("attrTitle").getValue();
this.field = element.attribute("attrName").getValue();
}

}






xml 样例:

<?xml version="1.0" encoding="utf-8"?>

<viewconfig fondsId="j010" name="年度视图" objectId="11" region="整理区" type="da_record">
<querys>
<query connector="and" field="da_year" fieldType="int" keyWords="2019" name="年度" operation="="/>
<query connector="and" field="da_year" fieldType="int" keyWords="2019" name="年度" operation="="/>
</querys>
<node attrName="da_year" attrTitle="年度">
<node attrName="da_bgqx" attrTitle="保管期限">
<node attrName="da_aaa" attrTitle="111"/>
</node>
</node>
</viewconfig>

原文地址:https://www.cnblogs.com/woshuaile/p/10858822.html