转自:
java把html标签字符转普通字符(反转换成html标签)
下文笔者讲述html标签转义和转义字符转换为html标签的方法分享,如下所示
实现思路:
借助spring包中org.springframework.web.util.HtmlUtils下的方法即可实现html字符的转义操作
HtmlUtils.htmlEscape
HtmlUtils.htmlUnescape
例:
package test.org.springframework.web.util;
import org.junit.Test;
import org.springframework.web.util.HtmlUtils;
/**
* 测试htmlUtils 功能
*/
public class TestHtmlUtils {
String html = "";
/**
* 把html的标签特殊字符转换成普通字符
*/
@Test
public void testhtmlEscape(){
String value = HtmlUtils.htmlEscape(html);
System.out.println(value);
}
/**
* 把html的特殊字符转换成普通数字
*/
@Test
public void testhtmlEscapeDecimal(){
String value = HtmlUtils.htmlEscapeDecimal(html);
System.out.println(value);
}
/**
* 把html的特殊字符转换成符合Intel HEX文件的字符串
*/
@Test
public void htmlEscapeHex(){
String value = HtmlUtils.htmlEscapeHex(html);
System.out.println(value);
}
/**
* 把html的特殊字符反转换成html标签
* 以上三种方法都可以反转换
*/
@Test
public void htmlUnescape(){
String tmp = HtmlUtils.htmlEscapeDecimal(html);
System.out.println(tmp);
String value = HtmlUtils.htmlUnescape(tmp);
System.out.println(value);
}
}