WebService Study Notes (iv)-SOAP message format and processing method

Source: Internet
Author: User
Tags soap
the format of a SOAP message

Two creation of SOAP messages

Java code:

1. Create message factory
messagefactory factory = Messagefactory.newinstance ();
2. Create SoapMessage
SoapMessage message = Factory.createmessage () According to the messaging factory;
3. Create Soappart
Soappart part = Message.getsoappart ();
4, obtain SoapEnvelope
SoapEnvelope envelope = Part.getenvelope ();
5, can be effective through SoapEnvelope to obtain the corresponding body and header information
soapbody BODY = Envelope.getbody ();
6. Create the corresponding node according to QName (QName is a node with a namespace)
//<ns:add xmlns= "Http://java.zttc.edu.cn/webservice"/>
QName QName = new QName ("Http://java.zttc.edu.cn/webservice", "Add", "ns");
If you use the following methods to set up, meet <> convert to &lt; and &gt
//body.addbodyelement (QName). SetValue ("<a>1</a> <b>2</b> ");
Soapbodyelement ele = body.addbodyelement (QName);
Ele.addchildelement ("a"). SetValue ("22"); Ele.addchildelement ("B"). SetValue ("the");
Print message information
Message.writeto (System.out);
three message passing

Java code:

1. Create service URL url = new URL (wsdlurl);
QName sname = new QName (ns, "Myserviceimplservice");

Service service = Service.create (url,sname); 2. Create Dispatch dispatch<soapmessage> Dispatch = Service.createdispatch (new QName (NS, "Myserviceimplport"),

Soapmessage.class,service.mode.message);
3. Create SoapMessage soapmessage msg = Messagefactory.newinstance (). CreateMessage ();
SoapEnvelope envelope = Msg.getsoappart (). Getenvelope ();

Soapbody BODY = Envelope.getbody (); 4. Create QName to specify the data passed in the message QName ename = new QName (ns, "Add", "NN"),//<nn:add xmlns= "xx"/> soapbodyelement ele =
Body.addbodyelement (ename);
Ele.addchildelement ("a"). SetValue ("22");
Ele.addchildelement ("B"). SetValue ("33");
Msg.writeto (System.out);

System.out.println ("\ n invoking ...");
5, pass the message through dispatch, will return the response message soapmessage response = Dispatch.invoke (msg);
Response.writeto (System.out);

System.out.println (); 6. Response message processing, convert the response message to a DOM object Document doc = Response.getsoappart (). Getenvelope (). GetBody (). Extractcontentasdocument ();
String str = doc.getelementsbytagname ("Addresult"). Item (0). Gettextcontent ();
 System.out.println (str);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.