java动态生成xml

public class CreateXML {
    //自定义  创建xml
    public String AddXml(List<CreateXmlEntity> list) {
        String xmlString = "";
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//转换日期
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.newDocument();
            document.setXmlStandalone(true);

            Element nodes = document.createElement("NODES");
            document.appendChild(nodes);

            for(CreateXmlEntity createXmlEntity:list) {
                // 此处可以循环添加
                Element node = document.createElement("NODE");
                nodes.appendChild(node);

                Element nodename = document.createElement("NODENAME");
                nodename.setTextContent(createXmlEntity.getNodeName());
                node.appendChild(nodename);

                Element step = document.createElement("STEP");
                step.setTextContent(createXmlEntity.getStep());
                node.appendChild(step);

                Element state = document.createElement("STATE");
                state.setTextContent(createXmlEntity.getState());
                node.appendChild(state);

                Element timelimit = document.createElement("TIMELIMIT");
                timelimit.setTextContent(createXmlEntity.getTimeLimit());
                node.appendChild(timelimit);

                Element starttime = document.createElement("STARTTIME");
                starttime.setTextContent(sdf.format(createXmlEntity.getStartTime()));
                node.appendChild(starttime);

                Element endtime = document.createElement("ENDTIME");
                endtime.setTextContent(sdf.format(createXmlEntity.getEndTime()));
                node.appendChild(endtime);

                Element staytime = document.createElement("STAYTIME");
                staytime.setTextContent(createXmlEntity.getStayTime()+"");
                node.appendChild(staytime);

                Element opinion = document.createElement("OPINION");
                opinion.setTextContent(createXmlEntity.getOpinion());
                node.appendChild(opinion);

                Element name = document.createElement("NAME");
                name.setTextContent(createXmlEntity.getName());
                node.appendChild(name);

                Element tel = document.createElement("TEL");
                tel.setTextContent(createXmlEntity.getTel());
                node.appendChild(tel);
                //二层
                Element specials = document.createElement("SPECIALS");
                node.appendChild(specials);

                if (createXmlEntity.getSpecialList() != null) {
                    for (SpecialEntity special1 : createXmlEntity.getSpecialList()) {
                        // 此处可以循环添加
                        Element special = document.createElement("SPECIAL");
                        specials.appendChild(special);

                        Element specialname = document.createElement("SPECIALNAME");
                        specialname.setTextContent(special1.getSpecialName());
                        special.appendChild(specialname);

                        Element specialstarttime = document.createElement("SPECIALSTARTTIME");
                        specialstarttime.setTextContent(sdf.format(special1.getSpecialBeginTime()));
                        special.appendChild(specialstarttime);

                        Element specialendtime = document.createElement("SPECIALENDTIME");
                        specialendtime.setTextContent(sdf.format(special1.getSpecialEndTime()));
                        special.appendChild(specialendtime);

                        Element specialreson = document.createElement("SPECIALRESON");
                        specialreson.setTextContent(special1.getReason());
                        special.appendChild(specialreson);
                    }

                }
            }

            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            DOMSource domSource = new DOMSource(document);

            // xml transform String
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            transformer.transform(domSource, new StreamResult(bos));
            xmlString = bos.toString();
            System.out.println(xmlString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return xmlString;
    }
    public static void main(String[] args) {
        CreateXML xml = new CreateXML();
        xml.AddXml(obj);
    }

}
结果展示:

<?xml version="1.0" encoding="UTF-8"?>
<NODES>
<NODE>
<NODENAME>受理</NODENAME>
<STEP>2</STEP>
<STATE>7</STATE>
<TIMELIMIT/>
<STARTTIME>2018-03-19 03:24:09</STARTTIME>
<ENDTIME>2018-03-19 03:24:09</ENDTIME>
<STAYTIME>0.0</STAYTIME>
<OPINION/>
<NAME>陈稳</NAME>
<TEL/>
<SPECIALS>
<SPECIAL>
<SPECIALNAME>补正</SPECIALNAME>
<SPECIALSTARTTIME>2018-03-19 03:24:54</SPECIALSTARTTIME>
<SPECIALENDTIME>2018-03-21 10:24:54</SPECIALENDTIME>
<SPECIALRESON>因材料不全或不符合要求</SPECIALRESON>
</SPECIAL>
</SPECIALS>
</NODE>
<NODE>
<NODENAME>接件</NODENAME>
<STEP>1</STEP>
<STATE>7</STATE>
<TIMELIMIT/>
<STARTTIME>2018-03-19 03:24:01</STARTTIME>
<ENDTIME>2018-03-19 03:24:01</ENDTIME>
<STAYTIME>0.0</STAYTIME>
<OPINION/>
<NAME>陈稳</NAME>
<TEL/>
<SPECIALS>
<SPECIAL>
<SPECIALNAME>补正</SPECIALNAME>
<SPECIALSTARTTIME>2018-03-19 03:24:54</SPECIALSTARTTIME>
<SPECIALENDTIME>2018-03-21 10:24:54</SPECIALENDTIME>
<SPECIALRESON>因材料不全或不符合要求</SPECIALRESON>
</SPECIAL>
</SPECIALS>
</NODE>
</NODES>

猜你喜欢

转载自blog.csdn.net/Wangnana0909/article/details/79726991