I've fixed my problem with a SSAAJOutInterceptor and a custom interceptor
where I get the soap message, find the elements with the attribute
xsi:nil="true" and remove them.

Here are the details if anyone else has this issue - it might be useful

CXF.xml

    <bean id="nillableSAAJInterceptor" class="NillableSAAJInterceptor"/>
    <bean id="saajOutInterceptor"
class="org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor"/>


    <cxf:bus>
        <cxf:features>
            <cxf:logging/>
        </cxf:features>
        <cxf:outInterceptors>            
            <ref bean="saajOutInterceptor"/>
            <ref bean="nillableSAAJInterceptor"/>
        </cxf:outInterceptors>
    </cxf:bus>


Customer interceptor

public class NillableSAAJInterceptor extends AbstractSoapInterceptor {

    public NillableSAAJInterceptor() {
        super(Phase.WRITE_ENDING);
    }

    String xpathString = "//*...@xsi:nil='true']";

    @Override
    public void handleMessage(SoapMessage message) throws Fault {
        SOAPMessage soapMessage = message.getContent(SOAPMessage.class);

        try {

            SOAPBody body = soapMessage.getSOAPBody();
            javax.xml.xpath.XPathFactory factory =
javax.xml.xpath.XPathFactory.newInstance();
            javax.xml.xpath.XPath xpath = factory.newXPath();
            xpath.setNamespaceContext(new XSINameSpaceContext());
            javax.xml.xpath.XPathExpression expression =
xpath.compile(xpathString);
            Object result = expression.evaluate(body,
XPathConstants.NODESET);
            NodeList nodeList = (NodeList)result;
            // Process the elements in the nodelist
            for (int i = 0; i < nodeList.getLength(); i++) {
                // Get element
                Element element = (Element) nodeList.item(i);
                logger.debug("element " + element.getNodeName());
                logger.debug("element " + element.getAttribute("xsi:nil"));
                // remove the element
                element.getParentNode().removeChild(element);
            }

        } catch (Exception e) {
            throw new Fault(e);
        }
    }
}

XSINameSpaceContext class

    public class XSINameSpaceContext implements NamespaceContext
        {
            public String getNamespaceURI(String prefix)
            {
                if (prefix.equals("xsi")){
                    return "http://www.w3.org/2001/XMLSchema-instance";;
                }
                else{
                    return XMLConstants.NULL_NS_URI;
                }
            }

            public String getPrefix(String namespace)
            {
                    return null;
            }

            public Iterator getPrefixes(String namespace)
            {
                return null;
            }
        }


-- 
View this message in context: 
http://old.nabble.com/Ignoring-nillables-tp28159451p28519317.html
Sent from the cxf-user mailing list archive at Nabble.com.

Reply via email to