这是我要发送给服务的信封:

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
        <sear:searchUrls>
            <sear:in0>Safeway, 200 S. 3rd St, Renton, WA</sear:in0>
        </sear:searchUrls>
    </soapenv:Body>
</soapenv:Envelope>


这是构造它的代码:

SOAPFactory fac = OMAbstractFactory.getSOAP12Factory();
SOAPEnvelope envelope = fac.getDefaultEnvelope();

OMNamespace omNs = fac.createOMNamespace("http://schemas.xmlsoap.org/soap/envelope/", "soapenv");
OMNamespace ns1 = fac.createOMNamespace("http://search", "sear");

envelope.setNamespace(omNs);

OMNamespace ns = fac.createOMNamespace("", "")
OMElement method = fac.createOMElement("sear:searchUrls", ns);
OMElement value = fac.createOMElement("sear:in0", ns);
value.setText("Safeway, 200 S. 3rd St, Renton, WA");
method.addChild(value);
envelope.getBody().addChild(method);


但是未定义我的名称空间前缀“ sear”。
我如何在代码中设置它以获得

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sear="http://search">


在XML中?

最佳答案

envelope.addNamespaceDeclaration("sear", "http://search");

09-27 23:08