引言
部分代码来自我引用的博客
需要导入的maven
<dependency>
<groupId>javax.xml.soap</groupId>
<artifactId>javax.xml.soap-api</artifactId>
<version>1.4.0</version>
</dependency>
从soapString转json
//导入所有相关的jar
import javax.xml.soap.*;
//String 转SoapMsg
public static SOAPMessage formatSoapString(String soapString) {
MessageFactory msgFactory;
try
{
msgFactory = MessageFactory.newInstance();
SOAPMessage reqMsg =
msgFactory.createMessage(new MimeHeaders(),
new ByteArrayInputStream(soapString.getBytes(Charset.forName("UTF-8"))));
reqMsg.saveChanges();
return reqMsg;
}
catch (Exception e)
{
return null;
}
}
//iterator 递归转json
private static void GetDate(Iterator<SOAPElement> iterator,JSONObject json) {
while (iterator.hasNext()) {
Object o = iterator.next();
if (o != null) {
SOAPElement element = null;
try {
element = (SOAPElement) o;
json.put(element.getNodeName(),element.getValue());
} catch (Exception e) {
}
if (element != null) {
GetDate(element.getChildElements(),json);
}
}
}
}
SOAPMessage msgs = formatSoapString(soapString);
SOAPBody body = null;
try {
SOAPEnvelope envelope = msgs.getSOAPPart().getEnvelope();
body = envelope.getBody();
} catch (SOAPException e) {
e.printStackTrace();
}
Iterator itr= body.getChildElements();
JSONObject json = new JSONObject();
GetDate(itr,json);
System.out.println(json.toJSONString());