Use java to convert SOAP message format to socket message format, and vice versa
•
Java
I am currently using java to study the conversion from SOAP message format to socket message format, and vice versa
I need this to reuse the legacy system that reads socket format messages to connect to websites that send and receive SOAP message formats
What shall I do? Should I consider word processing?
Sample socket to soap
Socket
<?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>
soap
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<webRequest xmlns="http://____________">
<arg0 xmlns=""><?xml version="1.0" encoding="UTF-8"
standalone="yes"?><Interface xmlns="http://____________"
Version="1.0" Code="20" Txn="123" CD="456"><Info
BirthDate="1983-03-27" Code="1234" Type="0" /></Interface></arg0>
</webRequest>
</soapenv:Body>
</soapenv:Envelope>
Solution
The socket message is the XML escape body of the SOAP message You don't need additional libraries because there is a javax class for parsing soap requests
Soap to socket is very simple:
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=\"\"><?xml version=\"1.0\" encoding=\"UTF-8\"\n standalone=\"yes\"?><Interface xmlns=\"http://____________\"\n Version=\"1.0\" Code=\"20\" Txn=\"123\" CD=\"456\"><Info\n BirthDate=\"1983-03-27\" Code=\"1234\" Type=\"0\" /></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 is more complicated because we need to create a webrequest wrapper element:
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);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
