JAXM 肥皂消息解析

Posted

技术标签:

【中文标题】JAXM 肥皂消息解析【英文标题】:JAXM soap message parsing 【发布时间】:2008-11-17 17:10:43 【问题描述】:

我正在从 .net 服务返回以下 XML:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <validateCredentialsResponse 
               xmlns="http://www.paragon.com/positionmonitor/PositionMonitor">
            <validateCredentialsResult>
                <ResultData xsi:type="ValidateCredentialsResultData">
                    <validated>true</validated>
                    <alreadyLoggedIn>false</alreadyLoggedIn>
                </ResultData>
                <Status>
                    <Condition xmlns="">SUCCESS</Condition>
                    <ErrorCode xmlns="">BO.00000</ErrorCode>
                    <ErrorDesc xmlns="">OK</ErrorDesc>
                </Status>
            </validateCredentialsResult>
        </validateCredentialsResponse>
    </soap:Body>
</soap:Envelope>

...我正在尝试使用 JAXM 对其进行解析,但是以下始终计算为 null:

SOAPEnvelope env = reply.getSOAPPart().getEnvelope();

谁能帮帮我?

【问题讨论】:

您需要提供更多详细信息。 “true false SUCCESS BO.00000 OK”看起来像来自 XML 的内部文本属性的输出 【参考方案1】:

我使用以下代码得到了不错的结果(至少需要 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);
      
    
  

【讨论】:

以上是关于JAXM 肥皂消息解析的主要内容,如果未能解决你的问题,请参考以下文章

使用 XmlDictionaryReader 反序列化肥皂消息

在“手动”发送肥皂消息时,我收到 WCF 的 AddressingNone 错误

如何使用 DataContractSerializer 从文件中反序列化 WCF 肥皂响应消息?

HTTP 标头或 SOAP 标头中的 WCF 肥皂操作?

如何在 wcf 中添加自定义肥皂标题?

WCF 服务客户端:定制的肥皂信封未在线发送