javafilewriterapache-commons-langstringescapeutilsapache-commons-lang3

Escape XML using stringutils commons.lang version3 apply on writer


In the older version of "org.apache.commons.lang"(2.6) StringEscapeUtils, there is a method to escapexml by passing an instance of "java.io.Writer" as one of the parameter.

eg: escapeXml(Writer writer, String str)

But, in newer version 3 StringEscapeUtils, no available method accepts an instance of "java.io.Writer" as one of the parameter.

Is there any alternative to use the writer in an indirect way, setup a callback or handler?


Solution

  • All the overloads accepting a Writer have been removed - but instead, CharSequenceTranslator instances are available, so you could write:

    StringEscapeUtils.ESCAPE_XML10.translate(input, writer);
    

    (Personally I'd strongly recommend using an XML-focused API to write XML rather than doing it "somewhat manually" like this, admittedly...)