RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 583380
Accepted
Nikolay Baranenko
Nikolay Baranenko
Asked:2020-10-27 17:08:07 +0000 UTC2020-10-27 17:08:07 +0000 UTC 2020-10-27 17:08:07 +0000 UTC

为什么SOAP请求中不广播属性值

  • 772

你好。

我有一个示例 SOAP 请求

         <?xml version="1.0" encoding="UTF-8" ?>
         <SOAP:Envelope xmlns:SOAP-ENV="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>
         <GetParameter xmlns="http://examples/2001">
		 <request>
         <MonCode xmlns="http://newsite/mon">Latency</MonCode>
		 <TimeFrom xmlns="http://newsite/mon">2016-10-26T11:00</TimeFrom>
		 <TimeTo xmlns="http://newsite/mon">2016-10-26T12:00</TimeTo>
         </request>
		 </GetParameter>
         </SOAP:Body>
         </SOAP:Envelope>

使用 SAAJ API 在 JAVA 中形成 SOAP 请求

以下代码

           MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage message = messageFactory.createMessage();
    SOAPPart soapPart = message.getSOAPPart();
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
    envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    envelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelop/");

    SOAPBody body = message.getSOAPBody();
    SOAPElement bodyElement = body.addChildElement("GetParameter");
    bodyElement.setAttribute("xmlns", "http://examples/2001");
    bodyElement = body.addChildElement("request");
    SOAPElement paramsElement = bodyElement.addChildElement("MonCode");
    paramsElement.setAttribute("xmlns", "http://newsite/mon");
    paramsElement.addTextNode("Latency");
    paramsElement = bodyElement.addChildElement("TimeFrom");
    paramsElement.setAttribute("xmlns", "http://newsite/mon");
    paramsElement.addTextNode("2016-10-26T11:00");
    paramsElement = bodyElement.addChildElement("TimeTo");
    paramsElement.setAttribute("xmlns", "http://newsite/mon");
    paramsElement.addTextNode("2016-10-26T12:00");

结果我收到这样的 SOAP 请求

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelop/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <GetParameter xmlns=""/>
    <request>
      <MonCode xmlns="">Latency</MonCode>
      <TimeFrom xmlns="">2016-10-26T11:00</TimeFrom>
      <TimeTo xmlns="">2016-10-26T12:00</TimeTo>
    </request>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

一切都很好,但是属性“ http://examples/2001 ”“ http://newsite/mon ”的值在某处消失了。

我的错误是什么以及如何解决这个问题?

java
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Nikolay Baranenko
    2020-10-28T16:50:27Z2020-10-28T16:50:27Z

    结果如下

           MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage soapMessage = messageFactory.createMessage();
    
    // 检索不同的部分
            SOAPPart soapPart = soapMessage.getSOAPPart();
            SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();
    
            soapEnvelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
            soapEnvelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    
    // 提取header的两种方式
        // SOAPHeader soapHeader = soapEnvelope.getHeader();
        // soapHeader = soapMessage.getSOAPHeader();
    
    // 两种提取body的方法
            SOAPBody soapBody = soapEnvelope.getBody();
            soapBody = soapMessage.getSOAPBody();
    
    // 添加一些元素
    
            SOAPElement GetParamter=soapBody.addBodyElement(new javax.xml.namespace.QName("http://examples/2001", "GetParamter"));
    
            GetParamter = GetParamter.addChildElement(new javax.xml.namespace.QName("request"));
            SOAPElement MonCode=GetParamter.addChildElement(new javax.xml.namespace.QName("http://newsite/mon", "MonCode"));
            MonCode.addTextNode("延迟");
    
            SOAPElement TimeFrom=GetParamter.addChildElement(new javax.xml.namespace.QName("http://newsite/mon", "TimeFrom"));
            TimeFrom.addTextNode("2016-10-26T11:00");
    
            SOAPElement TimeTo=GetParamter.addChildElement(new javax.xml.namespace.QName("http://newsite/mon", "TimeTo"));
            TimeTo.addTextNode("2016-10-26T12:00");
    

    生成的 SOAP 请求

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Header/>
    <SOAP-ENV:Body>
    <GetParamter xmlns="http://examples/2001">
    <request>
    <MonCode xmlns="http://newsite/mon">Latency</MonCode>
    <TimeFrom xmlns="http://newsite/mon">2016-10-26T11:00</TimeFrom>
    <TimeTo xmlns="http://newsite/mon">2016-10-26T12:00</TimeTo>
    </request>
    </GetParamter>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5