Java创建,解析XML片段文件,无根节点root

1. 创建xml文件

  • 包引用,需要导入如下三个包
    这里写图片描述
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.util.ArrayList;
import java.util.Collection;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import java.util.concurrent.TimeUnit;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
  • 具体实现
    public String createXmlInfo(String dst_ip) {

        // create xmlInfo pushed to snc
        String xmlStr = null;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.newDocument();
            document.setXmlVersion("1.0");

            Element aclGroup = document.createElement("aclGroup");
            document.appendChild(aclGroup);
            Element aclNumOrName = document.createElement("aclNumOrName");
            aclNumOrName.setTextContent("secflow");
            aclGroup.appendChild(aclNumOrName);
            Element aclStep = document.createElement("aclStep");
            aclStep.setTextContent("3");
            aclGroup.appendChild(aclStep);
            Element aclType = document.createElement("aclType");
            aclType.setTextContent("Advance");
            aclGroup.appendChild(aclType);
            Element aclRuleAdv4s = document.createElement("aclRuleAdv4s");
            Element aclRuleAdv4 = document.createElement("aclRuleAdv4");
            Element aclRuleID = document.createElement("aclRuleID");
            aclRuleID.setTextContent("4");
            aclRuleAdv4.appendChild(aclRuleID);
            Element aclProtocol = document.createElement("aclProtocol");
            aclProtocol.setTextContent("0");
            aclRuleAdv4.appendChild(aclProtocol);
            Element aclSourceIp = document.createElement("aclSourceIp");
            aclSourceIp.setTextContent("0.0.0.0");
            aclRuleAdv4.appendChild(aclSourceIp);
            Element aclSrcWild = document.createElement("aclSrcWild");
            aclSrcWild.setTextContent("0.0.0.255");
            aclRuleAdv4.appendChild(aclSrcWild);
            Element aclDestIp = document.createElement("aclDestIp");
            aclDestIp.setTextContent(dst_ip);
            aclRuleAdv4.appendChild(aclDestIp);
            Element aclDestWild = document.createElement("aclDestWild");
            aclDestWild.setTextContent("0.0.0.255");
            aclRuleAdv4.appendChild(aclDestWild);
            Element aclRuleName = document.createElement("aclRuleName");
            aclRuleName.setTextContent("rule1");
            aclRuleAdv4.appendChild(aclRuleName);
            aclRuleAdv4s.appendChild(aclRuleAdv4);
            aclGroup.appendChild(aclRuleAdv4s);

            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transFormer = null;
            try {
                transFormer = transFactory.newTransformer();
            } catch (TransformerConfigurationException e) {
                log.info("{}",e.getMessage());
            }
            DOMSource domSource = new DOMSource(document);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try {
                transFormer.transform(domSource, new StreamResult(bos));
            } catch (TransformerException e) {
                log.info("{}",e.getMessage());
            }

            org.dom4j.Document doc = null;
            try {
                doc = DocumentHelper.parseText(bos.toString());
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            String test = doc.node(0).asXML();
            xmlStr = test.replace("","").replace("","");
        }
        catch (ParserConfigurationException e) {
            log.info("{}",e.getMessage());
        }

        return xmlStr;
    }
  • 结果:生成xml数据如下
<aclStep>5aclStep>
<aclType>AdvanceaclType>
<aclRuleAdv4s>
     <aclRuleAdv4>
              <aclRuleID>1aclRuleID>
              <aclProtocol>0aclProtocol>
              <aclSourceIp>192.168.1.0aclSourceIp>
              <aclSrcWild>0.0.0.255aclSrcWild>
              <aclDestIp>192.168.2.0aclDestIp>
              <aclDestWild>0.0.0.255aclDestWild>
              <aclRuleName>rule1aclRuleName>
      aclRuleAdv4>    
aclRuleAdv4s>

你可能感兴趣的:(Java)