基于jsoup的Element实现将树形结构转换成XML格式

依赖

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.13.1</version>
</dependency>

树形的model类

public class SchemaElementModel {
    private SchemaElementModel parentEm;//上级元素标签名字
    private String tagName;//当前元素标签名字
    private String text;
    //当前节点的属性 例如  id="p-1627207" name="颜色分类" type="multiCheck"
    private Map<String, String> tagAttrMap = new LinkedHashMap<>();
    private List<SchemaElementModel> childEms;//子元素标签名字
    private final String pidStart = "p-";

    public SchemaElementModel() {
    }

    public SchemaElementModel(String tagName, Field field) {
        this.tagName = tagName;
        this.tagAttrMap.put("id", field.getId());
        this.tagAttrMap.put("name", field.getName());
        this.tagAttrMap.put("type", field.getType().value());
    }

    public SchemaElementModel(String tagName) {
        this.tagName = tagName;
    }

    public List<SchemaElementModel> getChildEms() {
        return childEms;
    }

    public void setChildEms(List<SchemaElementModel> childEms) {
        this.childEms = childEms;
    }

    public String getTagName() {
        return tagName;
    }

    public void setTagName(String tagName) {
        this.tagName = tagName;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Map<String, String> getTagAttrMap() {
        return tagAttrMap;
    }

    public void setTagAttrMap(Map<String, String> tagAttrMap) {
        this.tagAttrMap = tagAttrMap;
    }

    public void putTagAttrMap(String attrName, String attrVal) {
        this.tagAttrMap.put(attrName, attrVal);
    }

    public SchemaElementModel getParentEm() {
        return parentEm;
    }

    public void setParentEm(SchemaElementModel parentEm) {
        this.parentEm = parentEm;
    }

    public List<SchemaElementModel> selectChildEms(String tagName) {
        if (this.childEms == null || tagName == null) return null;
        return this.childEms.stream().filter(x -> tagName.equals(x.getTagName())).collect(Collectors.toList());
    }

    public SchemaElementModel addChildEm(SchemaElementModel model) {
        this.childEms = this.childEms == null ? new ArrayList<>() : this.childEms;
        this.childEms.add(model);
        return this;
    }

    public SchemaElementModel addChildEm(String childTagName) {
        this.childEms = this.childEms == null ? new ArrayList<>() : this.childEms;
        this.childEms.add(new SchemaElementModel(childTagName));
        return this;
    }

执行代码

public class ItemSchemaUtil {
    /**
     * 传入一个元素和自定义的对象model进来
     * 根据model的参数创建指定标签和标签的属性
     *
     * @param e  参数只要是实现了Element
     * @param em 自定义的model类
     */
    public static <E extends Element> void appendElement(E e, SchemaElementModel em) {
        if (e == null) return;
        Element element = e.appendElement(em.getTagName());//创建标签
        if (!CollectionUtils.isEmpty(em.getTagAttrMap())) {//设置标签属性
            em.getTagAttrMap().entrySet().forEach(x -> element.attr(x.getKey(), x.getValue()));
            //设置文本值例如<value>真不错</value>
            if (!StringUtils.isEmpty(em.getText())) element.text(em.getText());
        }

        if (em.getParentEm() != null) {//父标签
            Element pElement = new Element(em.getTagName());
            em.getTagAttrMap().entrySet().forEach(x -> pElement.attr(x.getKey(), x.getValue()));
            if (!StringUtils.isEmpty(em.getText())) pElement.text(em.getText());
            e.appendTo(pElement);
        }
        if (CollectionUtils.isEmpty(em.getChildEms())) return;//子标签
        for (SchemaElementModel elementModel : em.getChildEms()) {
            setElement(element, elementModel);
        }
    }

    /**
     * 子节点递归调用
     *
     * @param element
     * @param em
     */
    private static void setElement(Element element, SchemaElementModel em) {
        Element appendElement = element.appendElement(em.getTagName());
        if (!CollectionUtils.isEmpty(em.getTagAttrMap())) {
            em.getTagAttrMap().entrySet().forEach(x -> appendElement.attr(x.getKey(), x.getValue()));
            if (!StringUtils.isEmpty(em.getText())) appendElement.text(em.getText());
        }
        if (CollectionUtils.isEmpty(em.getChildEms())) return;
        for (SchemaElementModel elementModel : em.getChildEms()) {
            setElement(appendElement, elementModel);
        }

    }
}

SchemaElementModel的示例数据
在这里插入图片描述
生成的XML示例
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Acmen-zym

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值