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><Response><Execution><Status code=\"0\" sqlcode=\"0\" description=\"SC03-CT2402070001\"/></Execution><ResponseContent><Parameter/><Document/></ResponseContent></Response></fjs1:response></fjs1:cws_ins_axmt610Response></SOAP-ENV:Body></SOAP-ENV:Envelope>";
xml = xml.replaceAll("<", "<").replaceAll(">", ">");
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;
}
}
java处理soap
最新推荐文章于 2024-08-24 22:01:16 发布