Java 如何删除SOAPElement中的前缀和命名空间?

Java 如何删除SOAPElement中的前缀和命名空间?,java,xml,dom,wsdl,saaj,Java,Xml,Dom,Wsdl,Saaj,在讨论会上,我有一个使用Nessery结构创建soap xml的循环(不要问结构) 更新 我尝试配置xml元素: /*Config body elements*/ while (itBodyElements.hasNext()) { Object o = itBodyElements.next(); SOAPBodyElement bodyElem

在讨论会上,我有一个使用Nessery结构创建soap xml的循环(不要问结构)

更新

我尝试配置xml元素:

    /*Config body elements*/
                while (itBodyElements.hasNext()) 
                {  
                  Object o = itBodyElements.next();
                  SOAPBodyElement bodyElement = (SOAPBodyElement) o;
                  log.info("Elements from 'Body' element = " + bodyElement.getLocalName() );

                  Iterator it2 = bodyElement.getChildElements();
                         while (it2.hasNext()) 
                         { 
                              Object requestElement = it2.next();
                              SOAPBodyElement bodyRequest = (SOAPBodyElement) requestElement;
                              log.info("  Elements from '"+ bodyElement.getLocalName() + "' element = " + bodyRequest.getLocalName()); 
                              log.info("  Delete namespace from IN element " + bodyRequest.getLocalName());
                              bodyRequest.removeNamespaceDeclaration(bodyRequest.getPrefix());
                              bodyRequest.setPrefix("");

                               Iterator it3 = bodyRequest.getChildElements();
                                    while (it3.hasNext())
                                    { //work with other elements
但它对“in”元素没有影响。跑步后我仍然有:

更新

我通过调用ws as next解决了此问题:

getWebServiceTemplate().marshalSendAndReceive(
                "URL",
                request,
                new WebServiceMessageCallback()
                { public void doWithMessage(WebServiceMessage message) {

                        SaajSoapMessage saajSoapMessage = (SaajSoapMessage)message;

                        SOAPMessage soapMessage = UtilsClass.createSOAPMessage(in);

                        saajSoapMessage.setSaajMessage(soapMessage);

                }

                } 
                );

方法createSOAPMessage使用javax.xml.soap库配置soap消息。

您可以使用以下方法删除属性

    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nList = (NodeList)xPath.evaluate("/Envelope/Body/request/in", body, XPathConstants.NODESET);
    for (int i = 0; i < nList.getLength(); ++i) {
        Element e = (Element) nList.item(i);
        e.removeAttribute("xmlns");
    }
XPath=XPathFactory.newInstance().newXPath();
NodeList nList=(NodeList)xPath.evaluate(“/Envelope/Body/request/in”,Body,XPathConstants.NODESET);
对于(int i=0;i
下面的测试表明它确实有效

@Test
public void removeXmlns() throws Exception {
    String xml = "" +
            "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
            "   <soapenv:Body>\n" +
            "      <ns3:request xmlns:ns3=\"http://mayacomp/Generic/Ws\">\n" +
            "         <in xmlns=\"http://mayacomp/Generic/Ws\">\n" +
            "            <requestHeader>\n" +
            "            </requestHeader>\n" +
            "         </in>\n" +
            "      </ns3:request>\n" +
            "   </soapenv:Body>\n" +
            "</soapenv:Envelope>";


    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document document = builder.parse(ResourceUtils.getFile("/soaptest.xml").getAbsolutePath());
    Element body = document.getDocumentElement();

    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nList = (NodeList)xPath.evaluate("/Envelope/Body/request/in", body, XPathConstants.NODESET);
    for (int i = 0; i < nList.getLength(); ++i) {
        Element e = (Element) nList.item(i);
        e.removeAttribute("xmlns");
    }
    DOMSource domSource = new DOMSource(document);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    logger.info("XML IN String format is: \n" + writer.toString());     
}
@测试
public void removeXmlns()引发异常{
字符串xml=“”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
"";
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
Document Document=builder.parse(ResourceUtils.getFile(“/soaptest.xml”).getAbsolutePath();
元素体=document.getDocumentElement();
XPath=XPathFactory.newInstance().newXPath();
NodeList nList=(NodeList)xPath.evaluate(“/Envelope/Body/request/in”,Body,XPathConstants.NODESET);
对于(int i=0;i
输出是

2015-11-26-11-46-24[]::[main]:(demo.TestCode.removeXmlns(TestCode.java:174):174):INFO :TestCode:XML IN String format is: 
<?xml version="1.0" encoding="UTF-8" standalone="no"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns3:request xmlns:ns3="http://dfg/Ws">
         <in>
            <requestHeader>
            </requestHeader>
         </in>
      </ns3:request>
   </soapenv:Body>
</soapenv:Envelope>
2015-11-26-11-46-24[]::[main]:(demo.TestCode.removeXmlns(TestCode.java:174):174):信息:TestCode:XML字符串格式是:

如果我正确理解了问题,您的代码将获得以下XML作为输入:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns2:request xmlns:ns3="http://mayacomp/Generic/Ws">
         <ns2:in>
            <ns2:requestHeader>

您希望将其转换为以下XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns3:request xmlns:ns3="http://mayacomp/Generic/Ws">
         <in>
            <requestHeader>

在这种情况下,调整名称空间声明和名称空间前缀是不够的,因为在DOM中,
中的
requestHeader
元素仍将位于
http://mayacomp/Generic/Ws
名称空间。这是因为DOM元素的命名空间URI是在创建/解析时确定的,并且在以后添加或删除命名空间声明时不会更改。当DOM被序列化时,序列化程序将自动生成必要的名称空间声明,以确保输出中的元素有效地拥有DOM中的名称空间。这就是为什么会得到
xmlns=”http://mayacomp/Generic/Ws“
在输出中,尽管该名称空间声明在DOM中不存在


您真正需要做的是更改这些元素的名称空间URI。不幸的是,DOM节点没有
setNamespaceURI
方法,您需要使用
Document.renameNode

您知道这两段xml确实有不同的含义,对吗?在第一个示例中,
将位于命名空间
http://mayacomp/Generic/Ws
。在第二种情况下,它将位于默认名称空间中(无论之前的默认名称空间是什么),这将不起作用,因为在DOM中,元素仍将位于
http://mayacomp/Generic/Ws
namespace。示例传入的xml具有和。问题是如何删除第二个名称空间xmlns,这就是我的代码示例所做的。您使用的是不知道名称空间的解析,但OP使用的是SAAJ,它总是使用知道名称空间的解析器处理消息。当时肯定误解了这个问题,没有看到关于SAAJ的任何信息。刚刚回答了我认为是问题的地方。这个问题被标记为“saaj”,标题是指SOAPElement,代码使用了特定于saaj的方法…Andreas Vei然后,我添加了更新。你能看一下吗?你的更新没有考虑我在回答中解释的内容。因此,它仍然不起作用也就不足为奇了。
2015-11-26-11-46-24[]::[main]:(demo.TestCode.removeXmlns(TestCode.java:174):174):INFO :TestCode:XML IN String format is: 
<?xml version="1.0" encoding="UTF-8" standalone="no"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns3:request xmlns:ns3="http://dfg/Ws">
         <in>
            <requestHeader>
            </requestHeader>
         </in>
      </ns3:request>
   </soapenv:Body>
</soapenv:Envelope>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns2:request xmlns:ns3="http://mayacomp/Generic/Ws">
         <ns2:in>
            <ns2:requestHeader>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns3:request xmlns:ns3="http://mayacomp/Generic/Ws">
         <in>
            <requestHeader>