基于SAAJ的Web服务----(二)将SOAP信封写出到输出流

废话少说,直接代码。

	public void witeToOut() {
		try {
			SOAPMessage soapMsg = MessageFactory.newInstance().createMessage();
			SOAPPart soapPart = soapMsg.getSOAPPart();
			SOAPEnvelope env = soapPart.getEnvelope();
			SOAPBody body = env.getBody();

			String iNs = "http://hello";
			String elementName = "sayHello";
			QName isbnQName = new QName(iNs, elementName);

			body.addBodyElement(isbnQName).setValue("world");

			soapMsg.writeTo(System.out);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


运行结果如下:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<sayHello xmlns="http://hello">world</sayHello>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


这就是我们创建的SOAP信封啦...

你可能感兴趣的:(SAAJ)