HeaderElements必须是名称空间限定的

时间:2012-11-28 19:04:13

标签: java xml soap

您好我有一个java代码来创建一个SOAP消息来发送WSDL服务。代码如下:

  SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
  SOAPConnection connection = sfc.createConnection();

  MessageFactory mf = MessageFactory.newInstance();
  SOAPMessage sm = mf.createMessage();

  SOAPHeader sh = sm.getSOAPHeader();
  SOAPBody sb = sm.getSOAPBody();


  QName bodyName = new QName("SendSMSInput");
  SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);

  QName expiryDate = new QName("EXPIRY_DATE");
  SOAPElement node = bodyElement.addChildElement(expiryDate);
  node.addTextNode("TARIH GELECEK BURAYA");

  QName message_class = new QName("MESSAGE_CLASS");
  SOAPElement node2 = bodyElement.addChildElement(message_class);
  node2.addTextNode("MESSAGE_CLASS GELECEK BURAYA");

  QName s_date = new QName("S_DATE");
  SOAPElement node3 = bodyElement.addChildElement(s_date);
  node3.addTextNode("S_DATE GELECEK BURAYA");

  QName short_number = new QName("SHORT_NUMBER");
  SOAPElement node4 = bodyElement.addChildElement(short_number);
  node4.addTextNode("SHORT NUMBER GELECEK BURAYA");

  QName src_msisdn = new QName("SRC_MSISN");
  SOAPElement node5 = bodyElement.addChildElement(src_msisdn);
  node5.addTextNode("BOŞLUK");

  QName to_rec = new QName("TO_RECEIVERS");
  SOAPElement node6 = bodyElement.addChildElement(to_rec);


  QName msisdn = new QName("msisdn");
  SOAPElement node6_1 = node6.addChildElement(msisdn);
  node6_1.addTextNode("BOŞLUK");

  QName message_body = new QName("MESSAGE_BODY");
  SOAPElement node7 = bodyElement.addChildElement(message_body);


  QName message = new QName("message");
  SOAPElement node7_1 = node7.addChildElement(message);
  node7_1.addTextNode("BOŞLUK");

生成以下内容:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
    <SendSMSInput>
        <EXPIRY_DATE>TARIH GELECEK BURAYA</EXPIRY_DATE>
        <MESSAGE_CLASS>MESSAGE_CLASS GELECEK BURAYA</MESSAGE_CLASS>
        <S_DATE>S_DATE GELECEK BURAYA</S_DATE>
        <SHORT_NUMBER>SHORT NUMBER GELECEK BURAYA</SHORT_NUMBER>
        <SRC_MSISN>BOŞLUK</SRC_MSISN>
        <TO_RECEIVERS>
            <msisdn>BOŞLUK</msisdn>
        </TO_RECEIVERS>
        <MESSAGE_BODY>
            <message>BOŞLUK</message>
        </MESSAGE_BODY>
    </SendSMSInput>
</SOAP-ENV:Body>

我需要在标题下添加元素。当我尝试

QName tokenHeader = new QName("token");
  SOAPHeaderElement tokenElement = sh.addHeaderElement(tokenHeader);

它说:

HeaderElements must be namespace qualified
Exception in thread "main" com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: HeaderElements must be namespace qualified
    at com.sun.xml.internal.messaging.saaj.soap.impl.HeaderImpl.addHeaderElement(HeaderImpl.java:96)
    at soapgenerator.SOAPGenerator.main(SOAPGenerator.java:34)

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:4)

使用具有命名空间部分的QName构造函数而不是无命名空间构造函数。

  

QName(String namespaceURI,String localPart)

     

指定名称空间URI和本地部分的QName构造函数。