user1748166
user1748166

Reputation: 147

How to write into CDATA without escape characters

We have a CDataInterceptor in order to include a signed XML file into a soap envelope in a CDATA section.

But when we are executing the code we are getting the XML escaped, like & lt;CDATA instead of <[CDATA or & #xd; when a new line. This makes the service unable to consume the SOAP envelope because of invalid format.

The issue is here:

public class CDataXMLStreamWriter extends DelegatingXMLStreamWriter {

private static final Pattern XML_CHARS = Pattern.compile( "[&<>]" );
private static final String CDataOpen = "<![CDATA[";
private static final String CDataClose = "]]>";

public CDataXMLStreamWriter(XMLStreamWriter del) { 
    super(del);
} 

@Override 
public void writeCharacters(String text) throws XMLStreamException { 
    boolean useCData = XML_CHARS.matcher( text ).find();

    if (useCData) {
        super.writeCharacters(CDataOpen);
        super.writeCharacters(text);
        super.writeCharacters(CDataClose);
        //super.writeCData(text);
    }else { 
        super.writeCharacters(text); 
    } 
}

public void writeStartElement(String local) throws XMLStreamException { 
    super.writeStartElement(local); 
} 

}

writeCharacters method escapes all this special characters. Do you know any solution to add this XML keeping escaped characters like <, > etc.?

BR

Upvotes: 1

Views: 1568

Answers (0)

Related Questions