依赖

<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示例
在这里插入图片描述

GitHub 加速计划 / eleme / element
10
1
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:7 个月前 )
c345bb45 11 个月前
a07f3a59 * Update transition.md * Update table.md * Update transition.md * Update table.md * Update transition.md * Update table.md * Update table.md * Update transition.md * Update popover.md 11 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐