通过hutool生成如下xml文件
在这里插入图片描述
生成代码:

/**
	param list :数据库查询到的数据
*/
private static String createXml(List<Map<String, Object>> list) {
        Document doc = XmlUtil.createXml(); // 5.8+ 方法
        // ① 根元素(标准 SOAP 命名空间,URI 不能有空格)
        Element envelope = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "soapenv:Envelope");
        envelope.setAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
        envelope.setAttribute("xmlns:impl", "http://mes.baicmotor.com");
        doc.appendChild(envelope);

        // ② Header
        envelope.appendChild(doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "soapenv:Header"));

        // ③ Body
        Element body = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "soapenv:Body");
        envelope.appendChild(body);

        // ④ XGB01LIST(放在 impl 命名空间)
        Element xgbListRoot = doc.createElementNS("http://mes.baicmotor.com", "impl:XGB01LIST");
        body.appendChild(xgbListRoot);

        // ⑤ 循环放 <xgbList>(无命名空间,仅属性)
        for (Map<String, Object> map : list) {
            Element item = doc.createElementNS(null, "xgbList");
            item.setAttribute("vihcleType", map.get("vihcleType").toString());
            item.setAttribute("isGb", map.get("isGb").toString());
            xgbListRoot.appendChild(item);
        }

        String xml = XmlUtil.toStr(doc, false);
        return xml;
    }

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐