Clean up HTML so it doesn't break XML - Java XML

Java examples for XML:XML String Escape

Description

Clean up HTML so it doesn't break XML

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String in = "java2s.com";
        System.out.println(cleanup(in));
    }/*from  w w w .  j av a2s .c o  m*/

    /**
     * Clean up HTML so it doesn't break XML
     * @param in
     * @return
     */
    static String cleanup(String in) {
        String out = in.replaceAll("<!\\[CDATA\\[", "");
        out = out.replaceAll("\\]\\]>", "");
        out = out.replaceAll("&(?!amp;)", "&amp;");
        out = out.replaceAll("<br>", "<br />");
        out = out.replaceAll("<p>\\s*?</li>", "</p>\n</li>");
        return out;
    }
}

Related Tutorials