具有多个命名空间的Java SOAPElement

时间:2017-05-31 16:01:35

标签: java xml soap namespaces

这是我第一次使用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>

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

<?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();
    }
}

1 个答案:

答案 0 :(得分:0)

迟到的答案 - 如果这有助于其他人。我正在寻找类似问题的解决方案,在我的情况下,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");