java 使用dom4j 生成xml文件

164 阅读1分钟
  1. 依赖
<dependency>
    <groupId>org.dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>2.1.3</version>
</dependency>

2. 参数 数据结构

public class XmlOrderTicketAO {

   /**
    * 节点唯一标识
    */
   private Long id;

   /**
    * 标签名称
    */
   private String tag;

   /**
    * 标签值
    */
   private String tagValue;
   /**
    * 标签等级
    */
   private Integer level;
   /**
    * 标签属性
    */
   private List<TagAttribute> tagAttribute;

   /**
   * 子节点
   */
   private List<XmlOrderTicketAO>  children;
}


public class TagAttribute {
   /**
    * 属性名称
    */
   private String attriCode;
   /**
    * 属性值
    */
   private String attriVal;
}
  1. 代码实现

public String createXml(XmlOrderTicketAO entity) {

    Document document= DocumentHelper.createDocument();

    //先创建根节点
    Element rootElement = document.addElement(entity.getTag());
    //创建其他节点
    createElement(rootElement, entity);

    try {
        //创建格式化打印对象
        OutputFormat format = OutputFormat.createPrettyPrint();
        //创建文件输出流(会有文件未找到异常)
        FileOutputStream fileOutputStream=new FileOutputStream(path);
        //创建XML的(写)输出对象:放入"文件输出流"和"格式化打印"(编码不支持异常)
        XMLWriter xmlWriter=new XMLWriter(fileOutputStream,format);
        //把document对象输出(会有IO异常)
        xmlWriter.write(document);
        //关闭
        xmlWriter.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
*
*/
void createElement(Element element, XmlOrderTicketAO entity){

    if (StringUtils.isNotBlank(entity.getTagValue())){
        //设置标签的值
        element.addText(entity.getTagValue());
    }
    List<TagAttribute> tagAttributeList = entity.getTagAttribute();
    
    if (CollectionUtil.isNotEmpty(tagAttributeList)){
        for (TagAttribute tagAttribute : tagAttributeList) {
            //给标签添加属性和属性值
            element.addAttribute(tagAttribute.getAttriCode(),tagAttribute.getAttriVal());
        }
    }
    //递归处理子节点
    List<XmlOrderTicketAO> children = entity.getChildren();
    if (CollectionUtil.isNotEmpty(children)){
        for (XmlOrderTicketAO child : children) {
            Element childElement = element.addElement(child.getTag());
            createElement(childElement,child);
        }
    }
}


  1. 示例

请求参数:

{
    "id": 1729652361396,
    "tag": "A",
    "tagValue": "",
    "level": 1,
    "tagAttribute": [
        {
            "attriCode": "code",
            "attriVal": "123"
        }
    ],
    "children": [
        {
            "id": 1729652371904,
            "tag": "A1",
            "tagValue": "",
            "level": 2,
            "tagAttribute": [],
            "children": [
                {
                    "id": 1729652375611,
                    "tag": "A11",
                    "tagValue": "",
                    "level": 3,
                    "tagAttribute": [],
                    "children": [
                        {
                            "id": 1729652400387,
                            "tag": "A111",
                            "tagValue": "",
                            "level": 4,
                            "tagAttribute": [
                                {
                                    "attriCode": "name",
                                    "attriVal": "3"
                                }
                            ],
                            "children": []
                        }
                    ]
                },
                {
                    "id": 1729652383901,
                    "tag": "A12",
                    "tagValue": "",
                    "level": 3,
                    "tagAttribute": [],
                    "children": [
                        {
                            "id": 1729652410065,
                            "tag": "A121",
                            "tagValue": "",
                            "level": 4,
                            "tagAttribute": [
                                {
                                    "attriCode": "name",
                                    "attriVal": "33"
                                }
                            ],
                            "children": []
                        }
                    ]
                }
               
            ]
        }
    ]
}

结果

image.png

下载:

  1. servlet

  2. 浏览器打开,右键另存为。