users@saaj.java.net

Re: [SAAJ-USR] Removing xmlns attribute

From: V B Kumar Jayanti <Vbkumar.Jayanti_at_Sun.COM>
Date: Fri, 18 Mar 2005 19:20:58 +0530

Sami Lakka wrote:

> Hi,
>
>
>
>
>
> I have to remove xmlns attributes from SOAP stream using SAAJ.
> Basically the idea is that if I currently have following SOAP stream:
>
>
>
> <Value xsi:type="ns0:ArrayOfString">
>
> <string xmlns="http://www.w3.org/2001/XMLSchema">Value2</string>
>
> <string xmlns="http://www.w3.org/2001/XMLSchema">Value3</string>
>
> </Value>
>
>
>
> The stream should be after my handler as follows:
>
>
>
> <Value xsi:type="ns0:ArrayOfString">
>
> <string>Value2</string>
>
> <string>Value3</string>
>
> </Value>
>
>
>
>
>
> I found that the removing of the xmlns- attributes is easy but for
> some reason the namespace declaration is still in the stream when it
> is sent to the
>
> client. Isn't enough that the xmlns- attribute is removed or do I have
> to do something else. I'm using folloing code to test my algorithm
> (JWSDP-1.5)
>
>
>
>
>
> /*
>
> * Created on 18.3.2005
>
> */
>
> package tests.com.wa.xmlda.webservice.jwsdp;
>
>
>
> import java.util.Iterator;
>
>
>
> import javax.xml.soap.MessageFactory;
>
> import javax.xml.soap.Name;
>
> import javax.xml.soap.SOAPElement;
>
> import javax.xml.soap.SOAPException;
>
> import javax.xml.soap.SOAPFactory;
>
> import javax.xml.soap.SOAPMessage;
>
>
>
> import org.w3c.dom.Attr;
>
> import org.w3c.dom.NamedNodeMap;
>
> import org.w3c.dom.Node;
>
> import org.w3c.dom.NodeList;
>
>
>
> import com.wa.xmlda.ctxmanagement.IXMLDAConstants;
>
>
>
> /**
>
> * @author Sami Lakka
>
> */
>
> public class ArrayTester {
>
>
>
> public static void main(String[] args) {
>
> try {
>
>
> MessageFactory factory = MessageFactory.newInstance();
>
>
> SOAPMessage message = factory.createMessage();
>
>
>
>
> SOAPFactory soapFactory = SOAPFactory.newInstance();
>
>
> String[] arrayOfStrings = new String[] {"Value1","Value2",
>
>
> "Value3"};
>
>
>
> Name
> valueName = soapFactory.createName("Value", "ns0",
>
>
> IXMLDAConstants.XMLDA_NAMESPACE);
>
>
>
> Name
> typeAttributeName = soapFactory.createName("type", "xsi",
>
>
> "http://www.w3.org/2001/XMLSchema-instance");
>
>
>
>
> SOAPElement element = soapFactory.createElement(valueName);
>
>
>
> element.addAttribute(typeAttributeName, "ns0:ArrayOfString");
>
>
>
> for
> (int i = 0; i < arrayOfStrings.length; i++) {
>
>
> String stringItem = arrayOfStrings[i];
>
>
>
>
>
> Name name =
> soapFactory.createName("string","","http://www.w3.org/2001/XMLSchema");
>
>
>
> SOAPElement arrayCell = element.addChildElement(name);
>
>
> arrayCell.addTextNode(stringItem);
>
> }
>
>
>
>
> message.getSOAPBody().addChildElement(element);
>
>
>
>
>
>
> findNamespace(element);
>
>
> message.writeTo(System.out);
>
>
> System.out.write("\r\n".getBytes());
>
>
>
> } catch (SOAPException e) {
>
>
> e.printStackTrace();
>
> } catch (Exception e) {
>
>
> e.printStackTrace();
>
> }
>
> }
>
>
>
> private static void findNamespace(Node item) {
>
> NodeList children =
> item.getChildNodes();
>
>
>
>
> for (int i = 0; i
> <children.getLength(); i++) {
>
> Node
> child = children.item(i);
>
>
>
>
> if
> ("http://www.w3.org/2001/XMLSchema".equals(child.getNamespaceURI())) {
>
>
> SOAPElement elem = (SOAPElement) child;
>
>
>
>
>
> Iterator iter = elem.getAllAttributes();
>
>
>
>
>
> for (;iter.hasNext();) {
>
>
> Name name = (Name) iter.next();
>
>
>
> Attr attribute =
> elem.getAttributeNode(name.getLocalName());
>
>
>
> elem.removeAttributeNode(attribute);
>
>
> System.out.println("Removed attribute: "+attribute.getLocalName());
>
>
> }
>
>
> }
>
>
>
>
>
>
> NamedNodeMap attributes = child.getAttributes();
>
>
>
> if
> (attributes != null) {
>
>
> for (int j = 0; j < attributes.getLength(); j++)
> {
>
>
> findNamespace(attributes.item(j));
>
>
>
> }
>
> }
>
>
>
>
> findNamespace(child);
>
> }
>
> }
>
> }
>

Note sure exactly what your requirement is, but would the attached
program help. Its a modification of your code....

thanks...




import java.util.Iterator;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPMessage;

import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SAAJTest {

    public static final String XMLDA_NAMESPACE = "http://www.w3.org/2001/XMLSchema";

    public static void main(String[] args) throws Exception {

        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage message = factory.createMessage();

        SOAPFactory soapFactory = SOAPFactory.newInstance();
        String[] arrayOfStrings = new String[] {"Value1","Value2", "Value3"};

        Name valueName = soapFactory.createName("Value", "ns0", XMLDA_NAMESPACE);
        Name typeAttributeName = soapFactory.createName("type", "xsi", "http://www.w3.org/2001/XMLSchema-instance");

        SOAPElement element = soapFactory.createElement(valueName);
        element.addAttribute(typeAttributeName, "ns0:ArrayOfString");

        for (int i = 0; i < arrayOfStrings.length; i++) {
            String stringItem = arrayOfStrings[i];
            Name name = soapFactory.createName("string","","http://www.w3.org/2001/XMLSchema");
            SOAPElement arrayCell = element.addChildElement(name);
            arrayCell.addTextNode(stringItem);
        }

        SOAPElement elem = message.getSOAPBody().addChildElement(element);
        System.out.println("before....\n");
        message.writeTo(System.out);
        System.out.println("\n\n");
        findNamespace(elem);
        System.out.println("after....\n");
        message.writeTo(System.out);
        System.out.println("\n\n");
                                                                
    }

    private static void findNamespace(SOAPElement elem) throws Exception {
        SOAPFactory soapFactory = SOAPFactory.newInstance();
        elem.addAttribute(soapFactory.createName("xmlns", "", ""), "http://www.w3.org/2001/XMLSchema");
    }
}