加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

使用Java将SOAP消息格式转换为Socket消息格式转换,反之亦然

发布时间:2020-12-15 02:58:57 所属栏目:Java 来源:网络整理
导读:我目前正在使用 Java研究SOAP消息格式到Socket Message格式转换,反之亦然. 我需要这个来重用读取套接字格式消息的遗留系统来连接到发送和接收SOAP消息格式的网站. 我该怎么做?我应该考虑文字处理吗? 示例Socket到SOAP 插座 ?xml version="1.0" encoding="U
我目前正在使用 Java研究SOAP消息格式到Socket Message格式转换,反之亦然.

我需要这个来重用读取套接字格式消息的遗留系统来连接到发送和接收SOAP消息格式的网站.

我该怎么做?我应该考虑文字处理吗?

示例Socket到SOAP

插座

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Interface Code="20" 
      <Transaction Txn="01880120121024000001" CD="01880120121024000001001" 
     Date="2012-10-24 17:27:25"  BirthDate="1983-03-27" Code="8110009000000720" Type="0"/>
</Interface>

肥皂

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
 <soapenv:Body>
  <webRequest xmlns="http://____________">
   <arg0 xmlns="">&lt;?xml version="1.0" encoding="UTF-8"
    standalone="yes"?>&lt;Interface xmlns="http://____________"
    Version="1.0" Code="20" Txn="123" CD="456">&lt;Info
    BirthDate="1983-03-27" Code="1234" Type="0" />&lt;/Interface></arg0>
  </webRequest>
 </soapenv:Body>
</soapenv:Envelope>

解决方法

套接字消息是SOAP消息的XML转义主体.您不需要额外的库,因为有 parsing SOAP requests的javax类.

SOAP到socket非常简单:

String message = "<?xml version='1.0' encoding='UTF-8'?>n<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">n <soapenv:Body>n  <webRequest xmlns="http://____________">n   <arg0 xmlns="">&lt;?xml version="1.0" encoding="UTF-8"n    standalone="yes"?>&lt;Interface xmlns="http://____________"n    Version="1.0" Code="20" Txn="123" CD="456">&lt;Infon    BirthDate="1983-03-27" Code="1234" Type="0" />&lt;/Interface></arg0>n  </webRequest>n </soapenv:Body>n</soapenv:Envelope>";
InputStream is = new ByteArrayInputStream(message.getBytes());
SOAPMessage request = MessageFactory.newInstance().createMessage(null,is);
System.out.println(request.getSOAPBody().getTextContent());

Socket to SOAP更复杂,因为我们需要创建webRequest包装元素:

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setNamespaceAware(true); // webRequest needs a namespace
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

Document doc = docBuilder.newDocument();
Element root = doc.createElementNS("http://____________","webRequest");
doc.appendChild(root);

Element argElement = doc.createElement("arg0");
root.appendChild(argElement);
String message = "<?xml version="1.0" encoding="UTF-8" standalone="yes"?>n<Interface Code="20" n      <Transaction Txn="01880120121024000001" CD="01880120121024000001001" n     Date="2012-10-24 17:27:25"  BirthDate="1983-03-27" Code="8110009000000720" Type="0"/>n</Interface>";
argElement.setTextContent(message);

SOAPMessage request = MessageFactory.newInstance().createMessage();
request.getSOAPBody().addDocument(doc);
request.setProperty(SOAPMessage.WRITE_XML_DECLARATION,"true");
request.writeTo(System.out);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读