In addition to escaping certain XML characters, we need to replace a carriage return + a line feed, because when it's read back in, only the line feed is preserved. - Java XML

Java examples for XML:XML Encoding

Description

In addition to escaping certain XML characters, we need to replace a carriage return + a line feed, because when it's read back in, only the line feed is preserved.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String body = "java2s.com";
        System.out.println(escapeBody(body));
    }/*from w w  w. ja  v  a2s. com*/

    /**
     * In addition to escaping certain XML characters, we need to replace a carriage return + a line feed, because when
     * it's read back in, only the line feed is preserved. But flexrep isn't happy with the multipart data unless the
     * carriage returns are there as well.
     * 
     * @param body
     * @return
     */
    public static String escapeBody(String body) {
        body = body.replace("<", "&lt;");
        body = body.replace(">", "&gt;");
        body = body.replace("\r\n", "CARRIAGERETURN\n");
        return body;
    }
}

Related Tutorials