【问题标题】:Java SOAPElement With Multiple Namespaces具有多个命名空间的 Java SOAPElement
【发布时间】:2017-05-31 16:01:35
【问题描述】:

这是我第一次使用一般的 SOAP 请求和 XML,所以我可能会遗漏一些明显的东西。我无法在 SOAP 元素中显示几个命名空间之一。我需要这个:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/SoapEnvelope.xsd" 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
        <cuns:HeaderInfo xmlns:cuns="http://website.com/cuns">
            <cuns:Field1>123456</cuns:Field1>
            <cuns:Field2>987654321</cuns:Field2>
        </cuns:HeaderInfo>
    </soap:Header>
    <soap:Body>
        <n1:BodyField1
            xsi:schemaLocation="http://website.xsi/location" 
            xmlns:cuns="http://website.com/cuns" 
            xmlns:n1="http://website.com/n1" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <Transaction>
                ...
            </Transaction>

但我的输出给出了我的这个,尽管 BodyField1 上有 xsi:schemaLocation,但它缺少 xmlns:xsi。

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/SoapEnvelope.xsd" 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
        <cuns:HeaderInfo xmlns:cuns="http://website.com/cuns">
            <cuns:Field1>123456</cuns:Field1>
            <cuns:Field2>987654321</cuns:Field2>
        </cuns:HeaderInfo>
    </soap:Header>
    <soap:Body>
        <n1:BodyField1
            xmlns:cuns="http://website.com/cuns" 
            xmlns:n1="http://website.com/n1" 
            xsi:schemaLocation="http://website.xsi/location">
            <Transaction>
                ...
            </Transaction>

xmlns:xsi 减速适用于信封,cuns 和 n1 出现在 BodyField1 下。我的代码明确声明了 xmlns:xsi,然后是 xsi:schema。我不确定为什么它会显示 2 个其他名称空间和架构,而不是 xsi 名称空间。我尝试过命名空间的不同顺序,但这似乎并不重要。这是我的 BodyField1 代码:

public static void makeTransaction(Vector<Transaction> transactions, SOAPMessage message){

    DOMSource source = null;
    Element superRoot = null;
    SOAPBodyElement bodyRoot = null;
    SOAPEnvelope envelope = null;
    SOAPBody body = null;

    try {
        //Make the document
        envelope = message.getSOAPPart().getEnvelope();
        body = envelope.getBody();

        Name n1 = envelope.createName("BodyField1", "n1", 
        "http://website.com/n1");
        bodyRoot = body.addBodyElement(n1);

        bodyRoot.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        bodyRoot.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation", 
        "http://schemas.xmlsoap.org/soap/envelope/SoapEnvelope.xsd");
        bodyRoot.addNamespaceDeclaration("cuns", "http://website.com/cuns");

    } catch (SOAPException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
}

【问题讨论】:

    标签: java xml soap namespaces


    【解决方案1】:

    虽然迟到的答案 - 如果这对其他人有帮助。我正在寻找类似问题的解决方案,在我的情况下,xmlsns:xsi 即将到来,但 xsi:schemalocation 没有。下面为我​​做了诀窍。

    SOAPElement eleXSINs = envelope.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");
    eleXSINsfor .setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation", "http://xxxxxx.xsd");
    

    【讨论】:

      猜你喜欢
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 2017-04-29
      • 2013-06-03
      • 2020-01-03
      • 1970-01-01
      • 2014-02-07
      相关资源
      最近更新 更多