学习使用JAXM处理SOAP

       刚刚看完JAXM的教程,赶紧做个学习记录,以备以后的温习。

       我使用的是Myeclipse开发工具,包含了大部分jar包了,还差一个JAXM-API包需要另外加进来,我是在我安装的JEE5的文件夹里找到的。(在附件里供下载)

       使用JAXM发送SOAP有两种方法:SOAPConnection和Messaging providers,而编码SOAP的方法却是一样的,具体的还是在代码里体现吧。

    这段代码使用的是SOAPConnection:

public static void main(String[] args)
			throws UnsupportedOperationException, SOAPException {
		
		// Create SOAPConnection, two steps:
		SOAPConnectionFactory soapFactory = SOAPConnectionFactory.newInstance();
		SOAPConnection con = soapFactory.createConnection();

		// Create SOAPMessage
		MessageFactory messageFactory = MessageFactory.newInstance();
		SOAPMessage message = messageFactory.createMessage();
		
               // Create SOAPPart
		SOAPPart soapPart = message.getSOAPPart();
		// Create SOAPEnvelope
		SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
		// Create SOAPHeader
		SOAPHeader soapHeader = soapEnvelope.getHeader();
		soapHeader.detachNode();     //delete Header node
		// Create SOAPBody
		SOAPBody soapBody = soapEnvelope.getBody();
		Name bodyName = soapEnvelope.createName("getLastTradePrice", "m",
				"http://http://smartzxy.iteye.com");
		SOAPBodyElement gltp = soapBody.addBodyElement(bodyName);
		// insert content to <getLastTradePrice/>
		Name name=soapEnvelope.createName("Symbol");
		SOAPElement symbol=soapBody.addChildElement(name);
		symbol.addTextNode("SUNW");
		try {
			//Print the packed SOAP message
                       message.writeTo(System.out);
		} catch (IOException e) {
			 
			e.printStackTrace();
		}
               
               //Send SOAP message
		URLEndpoint endpoint=new URLEndpoint("http://smartzxy.iteye.com");
		SOAPMessage response=con.call(message, endpoint);
		con.close();
			
	}

 

    这段代码使用的是Messaging Provider:

public static void main(String[] args) throws NamingException,
			SOAPException, ParserConfigurationException, MalformedURLException {
		
		// Create Messaging Provider to send SOAP message
		Context ctx = new InitialContext();
		ProviderConnectionFactory pcFactory = (ProviderConnectionFactory) ctx
				.lookup("Test1");
		ProviderConnection pcCon = pcFactory.createConnection();

		// Get a Message Factory
		ProviderMetaData metaData = pcCon.getMetaData();
		String[] supportedProfiles = metaData.getSupportedProfiles();
		String profile = null;
		for (int i = 0; i < supportedProfiles.length; i++) {
			if (supportedProfiles[i].equals("ebxml")) {
				profile = supportedProfiles[i];
				break;
			}
		}
		MessageFactory factory = pcCon.createMessageFactory(profile);

		// Create SOAP message
		EbXMLMessageImpl message=(EbXMLMessageImpl)factory.createMessage();
		SOAPMessage message = factory.createMessage();
		
               SOAPPart soappart = message.getSOAPPart();
		

		// Create SOAP message from existed xml file
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		try {
			Document doc = dBuilder.parse("e:\\soap.xml");
			DOMSource domSource = new DOMSource(doc);
			soappart.setContent(domSource);
		} catch (SAXException e) {
			 
			e.printStackTrace();
		} catch (IOException e) {
			 
			e.printStackTrace();
		}

		// Add attachment
		String str = "Update address for Sunny Skies "
				+ "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439";
		AttachmentPart attachment = message.createAttachmentPart();
		attachment.setContent(str, "text/plain");
		attachment.setContentId("address");
		message.addAttachmentPart(attachment);

		//Add jpeg attachment 
               AttachmentPart attachment2=message.createAttachmentPart(); 
               byte[] jpegData=…; 
               ByteArrayInputString stream=new ByteArrayInputStream(jpegData);
		attachment2.setContent(stream,”image/jpeg”);
	        message.addAttachmentPart(attachment);
		
		//Another way to add jpeg attachment
               URL url = new URL("http://greatproducts.com/gizmos/img.jpg");
		DataHandler dh = new DataHandler(url);
		AttachmentPart attachment3 = message.createAttachmentPart(dh);
		attachment3.setContentId("gyro_image");
		message.addAttachmentPart(attachment);

		// Get attachment
		Iterator it = message.getAttachments();
		while (it.hasNext()) {
			AttachmentPart attachment4 = (AttachmentPart) it.next();
			Object content = attachment4.getContent();
			String id = attachment4.getContentId();
			System.out.print("Attachment " + id + " contains: " + content);
			System.out.println("");
		}

		pcCon.send(message);
		pcCon.close();
	}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值