Java封裝soap資料__Java

來源:互聯網
上載者:User
/** * 封裝soap資料 * @return * @throws SOAPException */public  String initSoapData() throws Exception{SOAPMessage soapmessage = MessageFactory.newInstance().createMessage();SOAPPart soappart = soapmessage.getSOAPPart();SOAPEnvelope soapenvelope = soappart.getEnvelope();SOAPHeader soapheader = soapenvelope.getHeader();soapenvelope.addNamespaceDeclaration("rec","http://cebpubservice.com/ReceiveService");SOAPElement id = soapheader.addChildElement("Token", "rec").addChildElement("Token", "rec");id.setTextContent("XXX內容。。。。。。");SOAPBody soapbody = soapenvelope.getBody();SOAPElement informres = soapbody.addChildElement("Receive","rec");informres.addChildElement("節點名稱","rec").setTextContent("M1420110010268");//一次添加更多內容informres.addChildElement("節點名稱","rec").setTextContent(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(new Date())+"+08:00");Source source = soappart.getContent();Node root = null;if (source instanceof DOMSource) {root = ((DOMSource) source).getNode();} else if (source instanceof SAXSource) {InputSource insource = ((SAXSource) source).getInputSource();DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();dbf.setNamespaceAware(true);Document doc = dbf.newDocumentBuilder().parse(insource);root = (Node) doc.getDocumentElement();}Transformer transformer = TransformerFactory.newInstance().newTransformer();transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8" );  transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.setOutputProperty( OutputKeys.METHOD, "html" ); DOMSource domsource = new DOMSource(root);StreamResult streamResult = new StreamResult();ByteArrayOutputStream baos = new ByteArrayOutputStream();streamResult.setOutputStream(baos);transformer.transform(domsource, streamResult);return baos.toString();}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.