具有多个命名空间的 Java SOAPElement [英] Java SOAPElement With Multiple Namespaces

查看:71
本文介绍了具有多个命名空间的 Java SOAPElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my first time working with SOAP requests and XML in general so I may be missing something obvious. I can not get one of a few namespaces to show in a SOAP element. I need this:

<?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.

But my output gives my this which is missing the xmlns:xsi despite there being xsi:schemaLocation on BodyField1.

<?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 代码:

The xmlns:xsi deceleration works fine for the envelope and cuns and n1 appear under BodyField1. My code explicitly declares xmlns:xsi and then xsi:schema. I'm not sure why it would show 2 other namespaces and the schema but not the xsi namespace. I have tried different ordering of the namespace, but it doesn't seem to matter. Here is my code for the 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();
    }
}

推荐答案

尽管回答晚了 - 如果这对其他人有帮助.我正在寻找类似问题的解决方案,在我的情况下,xmlsns:xsi 来了,但不是 xsi:schemalocation.下面对我有用.

Late answer though- if this helps others. I was hunting for solution for similar problem, in my case, xmlsns:xsi was coming but not the xsi:schemalocation. Below did the trick for me.

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");

这篇关于具有多个命名空间的 Java SOAPElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆