我使用以下代码得到了不错的结果(至少需要 Java 1.5):
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Jaxm {
private static List<Element> elements(NodeList nodes) {
List<Element> result = new ArrayList<Element>(nodes.getLength());
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node instanceof Element)
result.add((Element)node);
}
return result;
}
private static Element named(Element elem, String name) {
if (!elem.getNodeName().equals(name))
throw new IllegalArgumentException("Expected " + name + ", got " + elem.getNodeName());
return elem;
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException, SOAPException {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" \r\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \r\n" +
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n" +
" <soap:Body>\r\n" +
" <validateCredentialsResponse \r\n" +
" xmlns=\"http://www.paragon.com/positionmonitor/PositionMonitor\">\r\n" +
" <validateCredentialsResult>\r\n" +
" <ResultData xsi:type=\"ValidateCredentialsResultData\">\r\n" +
" <validated>true</validated>\r\n" +
" <alreadyLoggedIn>false</alreadyLoggedIn>\r\n" +
" </ResultData>\r\n" +
" <Status>\r\n" +
" <Condition xmlns=\"\">SUCCESS</Condition>\r\n" +
" <ErrorCode xmlns=\"\">BO.00000</ErrorCode>\r\n" +
" <ErrorDesc xmlns=\"\">OK</ErrorDesc>\r\n" +
" </Status>\r\n" +
" </validateCredentialsResult>\r\n" +
" </validateCredentialsResponse>\r\n" +
" </soap:Body>\r\n" +
"</soap:Envelope>\r\n" +
"";
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage msg = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8"))));
msg.saveChanges();
SOAPBody soapBody = msg.getSOAPBody();
for (Element response : elements(soapBody.getElementsByTagName("validateCredentialsResponse"))) {
for (Element result : elements(response.getElementsByTagName("validateCredentialsResult"))) {
List<Element> children = elements(result.getChildNodes());
Element resultData = named(children.get(0), "ResultData");
List<Element> resultDataChildren = elements(resultData.getChildNodes());
boolean validated = Boolean.getBoolean(named(resultDataChildren.get(0), "validated").getTextContent());
boolean alreadyLoggedIn = Boolean.getBoolean(named(resultDataChildren.get(1), "alreadyLoggedIn").getTextContent());
Element status = named(children.get(1), "Status");
List<Element> statusChildren = elements(status.getChildNodes());
String condition = named(statusChildren.get(0), "Condition").getTextContent();
String errorCode = named(statusChildren.get(1), "ErrorCode").getTextContent();
String errorDesc = named(statusChildren.get(2), "ErrorDesc").getTextContent();
System.out.printf("validated=%s alreadyLoggedIn=%s condition=%s errorCode=%s errorDesc=%s\n", validated, alreadyLoggedIn, condition, errorCode, errorDesc);
}
}
}
}