XML文件生成工具

XML文件生成工具

网上找了很多生成xml文件的代码,在文件比较简单的时候还可以,一旦嵌套层级比较深的时候就不行了。自己采用jdom实现了如下的版本。

将xml文件中每一个标签当做一个节点,该节点除name外还有可扩展的多个属性,该标签的值(可能是一个字符或数字,也可能是多个子节点)。

  • 节点实体类
class XMLDataNode{
    private String name;
    private Map attribute;
    private List subNodes;
    private String content;
    ...
    }

需要将数据构造成这样的树结构,然后递归遍历

  • 构造xml文件
public static void createXML(XMLDataNode root, String localPath){
        Element rootEle = new Element(root.getName());
        Map attribute = root.getAttribute();
        if(ObjectUtils.isMapNotEmpty(attribute)){
            for(String key : attribute.keySet()){
                rootEle.setAttribute(key, attribute.get(key).toString());
            }
        }
        if(StringUtils.isNotEmpty(root.getContent())){
            rootEle.setText(root.getContent());
        }

        getXmlFromList(rootEle, root.getSubNodes());

        Document doc = new Document(rootEle);
        Format format = Format.getPrettyFormat();
        XMLOutputter xmlOutputter = new XMLOutputter(format);

        File file = new File(localPath);

        try {
            OutputStream out = new FileOutputStream(file);
            xmlOutputter.output(doc, out);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 递归构造子节点
public static void getXmlFromList(Element root,List subList) {

        if(ObjectUtils.isListNotEmpty(subList)) {
            for (XMLDataNode dataNode : subList) {
                Element element = new Element(dataNode.getName());

                Map param = dataNode.getAttribute();
                if (ObjectUtils.isMapNotEmpty(param)) {
                    for (String key : param.keySet()) {
                        element.setAttribute(key, param.get(key).toString());
                    }
                }

                if(StringUtils.isNotEmpty(dataNode.getContent())){
                    element.setText(dataNode.getContent());
                }
                root.addContent(element);
                getXmlFromList(element, dataNode.getSubNodes());
            }
        }
    }
  • 举一个栗子
public static void main(String[] args) {

        XMLDataNode dataNode = new XMLDataNode();
        dataNode.setName("eventTypes");
        List abilityList = new ArrayList<>();
        XMLDataNode abilityNode = new XMLDataNode();
        abilityNode.setName("ability");
        Map attribute = new LinkedHashMap<>();
        attribute.put("comId", "abc");
        attribute.put("type","event_abc");
        attribute.put("expression", "abc.eventtype.ability.name.hhh");
        abilityNode.setAttribute(attribute);

        List resTypeList = new ArrayList<>();
        XMLDataNode resTypeNode = new XMLDataNode();
        resTypeNode.setName("resType");
        Map restTypeAttribute = new LinkedHashMap<>();
        restTypeAttribute.put("type", "model");
        restTypeAttribute.put("privilegeCode", "view");
        restTypeAttribute.put("dataSrc","");
        restTypeAttribute.put("expression", "");
        restTypeAttribute.put("dataUrl","");
        restTypeAttribute.put("linkageResourceModel", "dialogTriple");
        resTypeNode.setAttribute(restTypeAttribute);

        List groupList = new ArrayList<>();
        XMLDataNode groupNode = new XMLDataNode();
        groupNode.setName("eventTypeGroup");
        Map groupNodeAttribute = new LinkedHashMap<>();
        groupNodeAttribute.put("type", "base");
        groupNodeAttribute.put("expression", "abc.eventtype.efg.group.fff");
        groupNodeAttribute.put("display", "true");
        groupNode.setAttribute(groupNodeAttribute);

        List eventTypeList = new ArrayList<>();
        XMLDataNode eventTypeNode = new XMLDataNode();
        eventTypeNode.setName("eventType");
        eventTypeNode.setContent("1120001");
        Map eventTypeAttribute = new LinkedHashMap<>();
        eventTypeAttribute.put("order", "1");
        eventTypeAttribute.put("expression", "abc.eventtype.efg.group.fff.1120001");
        eventTypeNode.setAttribute(eventTypeAttribute);

        XMLDataNode eventTypeNode2 = new XMLDataNode();
        eventTypeNode2.setName("eventType");
        eventTypeNode2.setContent("1120002");
        Map eventTypeAttribute2 = new LinkedHashMap<>();
        eventTypeAttribute2.put("order", "2");
        eventTypeAttribute2.put("expression", "abc.eventtype.efg.group.fff.1120002");
        eventTypeNode2.setAttribute(eventTypeAttribute2);

        eventTypeList.add(eventTypeNode);
        eventTypeList.add(eventTypeNode2);
        groupNode.setSubNodes(eventTypeList);

        groupList.add(groupNode);
        resTypeNode.setSubNodes(groupList);
        resTypeList.add(resTypeNode);

        abilityNode.setSubNodes(resTypeList);
        abilityList.add(abilityNode);

        dataNode.setSubNodes(abilityList);

        createXML(dataNode, "d://test2.xml");
    }

效果展示
XML文件生成工具_第1张图片

感觉这个版本最大的问题是怎么构造数据,以及递归的效率问题。小方也不知道怎么优化了。

你可能感兴趣的:(工具使用,jdom)