Java如何将单引号和双引号编码为HTML实体? [英] Java how to encode single quote and double quote into HTML entities?

查看:192
本文介绍了Java如何将单引号和双引号编码为HTML实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将编码到" '进入'

我很惊讶单引号和双引号是未在HTML实体4.0中定义,因此 StringEscapeUtils 无法将这2个字符转义为相应的实体。

I am quite suprised single quote and double quote is not defined in HTML Entities 4.0, and so StringEscapeUtils not able to escape these 2 characters into respective entities.

是有没有其他与字符串相关的工具能够做到这一点?

Is there any other String related tool able to do this?

HTML实体4.0中没有定义单引号和双引号的原因?

Any reason why single quote and double quote is not defined in HTML Entities 4.0?

除了单引号和双引号之外,是否有任何框架能够将所有unicode字符编码为相应的实体?由于所有unicode都可以手动转换为十进制实体并以HTML格式显示,所以很奇怪是否有任何工具能够自动转换吗?

Besides single quote and double quote, is there any framework able to encode all the unicode character into respective entities? Since all the unicode can be manually translate into decimal entities and show in HTML, so wonder is there any tool able to convert it automatically?

推荐答案



  1. 单引号和双引用未在HTML 4.0中定义


仅限单引号未在HTML 4.0中定义,双引号定义为& quot; 启动HTML2.0

Single quote only is not defined in HTML 4.0, double quote is defined as " starting HTML2.0



  1. StringEscapeUtils无法将这2个字符转换为相应的实体


escapeXml11 in StringEscapeUtils 支持将单引号转换为&';

escapeXml11 in StringEscapeUtils supports converting single quote into '.

例如:

StringEscapeUtils.escapeXml11("'"); //Returns '
StringEscapeUtils.escapeHtml4("\""); //Returns "





  1. 还有其他与字符串相关的工具能够做到吗?


HTMLUtils 负责单引号和双引号,它还将值转换为十进制(如&#39 ; & " )。
以下示例取自对此问题

HTMLUtils from Spring framework takes care of single quotes & double quotes, it also converts the values to decimal (like ' & "). Following example is taken from the answer to this question:

import org.springframework.web.util.HtmlUtils;
[...]
HtmlUtils.htmlEscapeDecimal("&")` //gives &
HtmlUtils.htmlEscape("&")` //gives &





  1. HTML实体4.0中未定义单引号和双引号的原因是什么?


根据字符HTML 4中的实体引用未定义单引号。双引号可从HTML2.0获得。而单引号作为 XHTML1.0 的一部分得到支持。

As per Character entity references in HTML 4 the single quote is not defined. Double quote is available from HTML2.0. Whereas single quote is supported as part of XHTML1.0.



  1. 将所有unicode字符编码为相应实体的工具或方法


有一个非常好的&作为对此答案的一部分提到的简单java实现问题

There is a very good & simple java implementation mentioned as part of an answer to this question.

以下是基于该答案的示例程序:

Following is a sample program based on that answer:

import org.apache.commons.lang3.StringEscapeUtils;

public class HTMLCharacterEscaper {
    public static void main(String[] args) {        
        //With StringEscapeUtils
        System.out.println("Using SEU: " + StringEscapeUtils.escapeHtml4("\" ¶"));
        System.out.println("Using SEU: " + StringEscapeUtils.escapeXml11("'"));

        //Single quote & double quote
        System.out.println(escapeHTML("It's good"));
        System.out.println(escapeHTML("\" Grit \""));

        //Unicode characters
        System.out.println(escapeHTML("This is copyright symbol ©"));
        System.out.println(escapeHTML("Paragraph symbol ¶"));
        System.out.println(escapeHTML("This is pound £"));      
    }

    public static String escapeHTML(String s) {
        StringBuilder out = new StringBuilder(Math.max(16, s.length()));
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&' || c == '\'') {
                out.append("&#");
                out.append((int) c);
                out.append(';');
            } else {
                out.append(c);
            }
        }
        return out.toString();
    }

}

以下是一些有趣的链接,我在追求答案时遇到过:

Following are some interesting links, which i came across during the pursuit of the answer:

  • Common HTML entities used for typography
  • Why shouldn't &apos; be used to escape single quotes?
  • The Named Character Reference &apos;
  • HTML apostrophe

这篇关于Java如何将单引号和双引号编码为HTML实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆