You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中为指定XML添加SOAP信封并修改节点名称

将XML包装进SOAP信封并修改标签的Java实现

下面是完整的实现方案,包含解析原XML、修改标签名、嵌入SOAP信封的完整代码:

完整代码

import javax.xml.soap.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringReader;
import java.io.StringWriter;

public class SoapRequestWrapper {
    public static void main(String[] args) throws Exception {
        // 初始化SOAP消息结构
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();
        SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
        // 设置SOAP命名空间前缀为soapenv
        soapEnvelope.setPrefix("soapenv");
        SOAPBody soapBody = soapEnvelope.getBody();
        soapBody.setPrefix("soapenv");

        // 原XML内容
        String originalXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<getGeoCodesRequest>\n" +
                "    <Addrs>\n" +
                "        <City>DENVER</City>\n" +
                "        <Country>UNITED STATES</Country>\n" +
                "        <PostCd>80212</PostCd>\n" +
                "        <State>COLORADO</State>\n" +
                "    </Addrs>\n" +
                "</getGeoCodesRequest>";

        // 解析原XML并修改标签名
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(new StringReader(originalXml));
        doc.getDocumentElement().normalize();

        // 重命名Addrs -> addrs
        Element addrsElem = (Element) doc.getElementsByTagName("Addrs").item(0);
        Node renamedAddrs = doc.renameNode(addrsElem, null, "addrs");

        // 重命名各子标签
        doc.renameNode((Element) renamedAddrs.getElementsByTagName("City").item(0), null, "city");
        doc.renameNode((Element) renamedAddrs.getElementsByTagName("Country").item(0), null, "cntry");
        doc.renameNode((Element) renamedAddrs.getElementsByTagName("PostCd").item(0), null, "postCd");
        doc.renameNode((Element) renamedAddrs.getElementsByTagName("State").item(0), null, "statePrv");

        // 将修改后的XML节点导入SOAP Body
        Node importedNode = soapPart.importNode(doc.getDocumentElement(), true);
        soapBody.appendChild(importedNode);

        // 保存SOAP消息变更
        soapMessage.saveChanges();

        // 转换为字符串并移除XML声明
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(soapMessage.getSOAPPart()), new StreamResult(writer));

        // 输出最终SOAP请求
        System.out.println(writer.toString());
    }
}

关键步骤说明

  1. SOAP结构初始化:创建基础的SOAP信封、Body结构,设置soapenv前缀以匹配期望输出
  2. XML解析与标签修改:使用DOM API解析原XML,通过renameNode方法替换指定标签名
  3. 嵌入SOAP Body:将修改后的XML根节点导入SOAP Body,确保节点属于当前SOAP文档的DOM树
  4. 输出处理:通过Transformer移除XML声明,得到符合要求的纯SOAP信封内容

内容的提问来源于stack exchange,提问作者Pawan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 10:45:18