JAVA当中的Document

JAVA当中的Document分为W3c版本和org.dom4j
网上的W3c版本讲解很多
在这里着重讲org.dom4j的用法


        org.dom4j.Document document = ProtocolUtil.newSuccessDocument(ProtocolUtil.SYNC_TAG);
        org.dom4j.Element rootElement = document.getRootElement();
        org.dom4j.Element caseVector = rootElement.addElement("caseVector");
        org.dom4j.Element caseStruct = caseVector.addElement("caseStruct");
        caseStruct.addElement("taskId111");
        caseStruct.addElement("taskId").addText("111");
        org.dom4j.Element fileInfoElement = null;
        fileInfoElement = caseStruct.addElement("fileInfo");
        fileInfoElement.addElement("caseId").addText("案件id");
        fileInfoElement.addElement("ip").addText("我是ip");
        fileInfoElement.addElement("port").addText(String.valueOf(BusinessServerPortConstant.FILEANYWHERE_PORT));
        fileInfoElement.addElement("intact").addText("我是intact");
        org.dom4j.Element evidenceVector = fileInfoElement.addElement("evidenceVector");

        String xml = document.asXML();

        System.out.println(xml);
    

这段代码的执行结果:

XML CreateDocument success!
<?xml version="1.0" encoding="UTF-8"?>
<sync>
<returnCode>0</returnCode>
<returnDescription>ok</returnDescription>
<caseVector>
<caseStruct>
<taskId111/>
<taskId>111</taskId>
<fileInfo>
<caseId>案件id</caseId>
<ip>我是ip</ip>
<port>18479</port>
<intact>我是intact</intact>
<evidenceVector/>
</fileInfo>
</caseStruct>
</caseVector>
</sync>

方法当中有公司的代码封装请忽略。着重看怎么向xml当中插入标签和给标签内赋值

你可能感兴趣的:(JAVA当中的Document)