1

.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();

誰か助けてくれませんか?

4

1 に答える 1

4

次のコードを使用すると、良い結果が得られました (少なくとも 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);
      }
    }
  }
}
于 2010-06-01T18:50:37.887 に答える