java处理soap

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.xml.soap.*;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import net.sf.json.JSONObject;

    /**
    * 解析类
    */
public class XMLDone {
	
	public static void main(String[] args) throws UnsupportedEncodingException, IOException, SOAPException {
		 String Str = new String("www.google.com-www.google.com");
	     System.out.print("匹配成功返回值 :" );
	     System.out.println(Str.replaceAll("google", "runoob" ));
	     System.out.print("匹配失败返回值 :" );
	     System.out.println(Str.replaceAll("taobao", "runoob" ));
		JSONObject jsonObject = XML("");
	}
	
	//解析xml并封装成json对象返回
	public synchronized static JSONObject XML(String xmlDoc) throws UnsupportedEncodingException, IOException, SOAPException {
		JSONObject jsonObject = new JSONObject();
		System.out.println("开始测试");
		String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.ws.future.com/\">   <soapenv:Header>   \t\t<token>abc</token>\t</soapenv:Header>   <soapenv:Body>\t111   </soapenv:Body></soapenv:Envelope>";
        SOAPMessage soapMessage = formatSoapString(xml);
        SOAPHeader soapHeader = soapMessage.getSOAPHeader();
        NodeList token = soapHeader.getElementsByTagName("token");
        Node node = token.item(0);
        String hToken = node.getTextContent();
        System.out.println("======头部:token:");
        System.out.println("===" + hToken + "===");
        SOAPBody soapBody = soapMessage.getSOAPBody();
        System.out.println("======body:");
        System.out.println("===" + soapBody.getTextContent().trim().replaceAll("\r|\n|\t","") + "===");
        
        //xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body>1<fjs1:cws_ins_axmt610Response xmlns:fjs1=\"http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay\"><fjs1:response><Response><Execution>2<Status code=\"0\" sqlcode=\"0\" description=\"SC03-CT2402070001\">aa</Status>3</Execution><ResponseContent>4<Parameter/>5<Document/>6</ResponseContent></Response></fjs1:response></fjs1:cws_ins_axmt610Response>7</SOAP-ENV:Body></SOAP-ENV:Envelope>";
        xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><fjs1:cws_ins_axmt610Response xmlns:fjs1=\"http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay\"><fjs1:response>&lt;Response&gt;&lt;Execution&gt;&lt;Status code=\"0\" sqlcode=\"0\" description=\"SC03-CT2402070001\"/&gt;&lt;/Execution&gt;&lt;ResponseContent&gt;&lt;Parameter/&gt;&lt;Document/&gt;&lt;/ResponseContent&gt;&lt;/Response&gt;</fjs1:response></fjs1:cws_ins_axmt610Response></SOAP-ENV:Body></SOAP-ENV:Envelope>";
        xml = xml.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
        SOAPMessage soapMessage1 = formatSoapString(xml);
        //SOAPHeader soapHeader1 = soapMessage1.getSOAPHeader();
        SOAPBody soapBody1 = soapMessage1.getSOAPBody();
        System.out.println("soapBody1=="+soapBody1.getTextContent());
        
        NodeList statusList = soapBody1.getElementsByTagName("Status");
        for (int i = 0; i < statusList.getLength(); i++) {
//        	 Node nodeList = statusList.item(i);
//        	 String value = nodeList.getTextContent();
//        	 System.out.println("Value: " + value);
        	Element element = (Element)statusList.item(i);
        	String code = element.getAttribute("code");
        	System.out.println("标签内部元素解析结果code=="+code);
        	String description = element.getAttribute("description");
        	System.out.println("标签内部元素解析结果description=="+description);
		}
        Node statusNode = statusList.item(0);
        System.out.println(statusNode.getNodeName()+"--"+statusNode.getTextContent()+"~~");
		return jsonObject;
	}
	
	public synchronized static SOAPMessage formatSoapString(String soapString) throws UnsupportedEncodingException, IOException{
		MessageFactory msgFactory;
		try {
			msgFactory = MessageFactory.newInstance();
			SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),new ByteArrayInputStream(soapString.getBytes("UTF-8")));
			reqMsg.saveChanges();
			return reqMsg;
		} catch (SOAPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
}
  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值