0
<Request>
  <EMPId>?</EMPId>
</Request>

I know this is a repeated question, but i would like to post it again as i dint get a convincing answer from any of the threads i went through.

My ultimate aim is to add the XML given above as the Body content of a SOAP message. You can have a look at the following link to see how i am doing it. Namespace related error on creating SOAP Request It worked fine when i was using the Websphere Application Server 7.0 library.JRE is also present, forgot to include in screen shot.

With Websphere runtime (JRE is also present, forgot to include)

Since i have to export it as a jar and run it as a stand alone application, i have to remove the dependency of 'Websphere Application Server 7.0 library'. Because, by keeping this library, my jar size will go above 100MB. So i thought of taking only the library which i needed.

'com.ibm.ws.prereq.soap.jar'

With out Websphere runtime

Now the issue is, the Request tag of the generated SOAP request is coming in following format.

<Request xmlns="">
  <EMPId>?</EMPId>
</Request>

I am able to create a 'org.w3c.dom.Document' representation for the generated SOAP message. Now, can any one tell me how can I delete the xmlns="" from Request tag.

Community
  • 1
  • 1
Renjith
  • 1,122
  • 1
  • 19
  • 45
  • what is the expected namespace for the Request element? – jtahlborn Aug 27 '14 at 13:17
  • I declared the namespace 'http://myservice.test.com/2013/8/v1.0' for Body, . I i belive it will be default one for whatever coming under it. So i dont need a seperate namespace for Request. – Renjith Aug 27 '14 at 13:51
  • then you need to set that as the namespace on those elements when you add them to the dom. – jtahlborn Aug 27 '14 at 13:53
  • How do i do that?. Help required. my code is given in other thread.[http://stackoverflow.com/questions/25479797/namespace-related-error-on-creating-soap-request] – Renjith Aug 27 '14 at 13:58
  • again, it works fine when i have websphere runtime library in classpath. how does that works? – Renjith Aug 27 '14 at 14:00
  • you haven't shown any code, so i have no clue how it works. – jtahlborn Aug 27 '14 at 14:05
  • Please look at this thread [http://stackoverflow.com/questions/25479797/namespace-related-error-on-creating-soap-request]. thats the code i am working on – Renjith Aug 27 '14 at 14:08
  • Possible duplicate of [Namespace related error on creating SOAP Request](https://stackoverflow.com/questions/25479797/namespace-related-error-on-creating-soap-request) – Leonardo Alves Machado May 23 '17 at 15:13

2 Answers2

0

The simplest way what i found is:

first: in child set nasmespace as in root: second: remove namespace

 Document doc = new Document();
        Namespace xmlns = Namespace.getNamespace("http://www.microsoft.com/networking/WLAN/profile/v1");
        Element rootXML = new Element("WLANProfile", xmlns);


        Element nameXML = new Element("name");
            nameXML.addContent(name);
        rootXML.addContent(nameXML);
//below solution
            nameXML.setNamespace(xmlns);
            nameXML.removeNamespaceDeclaration(xmlns);
Paweł
  • 205
  • 2
  • 11
0

Finally I found several solutions of the described problem.

First, you can remove all namespaces from all xml using this answer.

Second, if you do not need to remove all namespaces in Xml, but only empty ones, they arise due to the fact that some namespace is written in the root elements, which is not in the child. For example:

<Request xmlns="http://пф.рф/КСАФ/2018-04-03"
        xmlns:AF4="xx"...>
   <EMP xmlns="">   
   ...
   </EMP>

So you need to set the same namespace for all children of root elements. It can be done using this code (call setTheSameNamespaceForChildren(rootElement) for root element before saving):

    private static final String namespaceKey = "xmlns";
    private static String namespaceValue;

    public static void setTheSameNamespaceForChildren(Element rootEl) {
        namespaceValue = rootEl.getAttribute(namespaceKey);
        NodeList list = rootEl.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node child = list.item(i);
            setTheSameNamespaceRecursively(child);
        }
    }

    private static void setTheSameNamespaceRecursively(Node node) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            boolean isChanged = setTheSameNamespace((Element) node);
            if (isChanged) {
                NodeList list = node.getChildNodes();
                for (int i = 0; i < list.getLength(); i++) {
                    Node child = list.item(i);
                    setTheSameNamespaceRecursively(child);
                }
            }
        }
    }
    private static boolean setTheSameNamespace(Element node) {
        String curValue = node.getAttribute(namespaceKey);
        if (curValue.length() == 0) {
            node.setAttribute(namespaceKey, namespaceValue);
            return true;
        }
        return false;
    }