The entity “miot“ was referenced, but not declared

实体被引用但未被声明

项目中有转PDF的操作,使用的是fremark的ftl文件进行解析,但是当文本中含有特殊符号如:& .解析就会报错,后面查了一下说是需要转义成 类似 & 这样的符号才能被html识别,于是查了一下,将常见的特殊符号进行了转义,代码如下:

/**
     * 将字符串中的XML特殊字符转义
     * & -> &
     * < -> &lt;
     * > -> &gt;
     * " -> &quot;
     * ' -> &apos;
     * · -> &middot;
     * **/
    public static String covertXML(String content) {
        char[] originContentChars = content.toCharArray();
        int finalContentCharLength = 0;//转义后字符串长度
        for (char contentChar : originContentChars) {
            switch (contentChar) {
                case '&':
                    finalContentCharLength = finalContentCharLength + 5;
                    continue;
                case '"':
                case '\'':
                case '·':
                    finalContentCharLength = finalContentCharLength + 6;
                    continue;
                case '<':
                case '>':
                    finalContentCharLength = finalContentCharLength + 4;
                    continue;
                default:
                    finalContentCharLength = finalContentCharLength + 1;
            }

        }
        char[] newContentChars = new char[finalContentCharLength];
        int newContentCharsPos = 0;
        for (char originContentChar : originContentChars) {
            switch (originContentChar) {
                case '&':
                    //& -> &amp;
                    newContentChars[newContentCharsPos] = '&';
                    newContentChars[newContentCharsPos + 1] = 'a';
                    newContentChars[newContentCharsPos + 2] = 'm';
                    newContentChars[newContentCharsPos + 3] = 'p';
                    newContentChars[newContentCharsPos + 4] = ';';
                    newContentCharsPos += 5;
                    continue;
                case '"':
                    //" -> &quot;
                    newContentChars[newContentCharsPos] = '&';
                    newContentChars[newContentCharsPos + 1] = 'q';
                    newContentChars[newContentCharsPos + 2] = 'u';
                    newContentChars[newContentCharsPos + 3] = 'o';
                    newContentChars[newContentCharsPos + 4] = 't';
                    newContentChars[newContentCharsPos + 5] = ';';
                    newContentCharsPos += 6;
                    continue;
                case '\'':
                    //' -> &apos;
                    newContentChars[newContentCharsPos] = '&';
                    newContentChars[newContentCharsPos + 1] = 'a';
                    newContentChars[newContentCharsPos + 2] = 'p';
                    newContentChars[newContentCharsPos + 3] = 'o';
                    newContentChars[newContentCharsPos + 4] = 's';
                    newContentChars[newContentCharsPos + 5] = ';';
                    newContentCharsPos += 6;
                    continue;
                case '·':
                    //· -> &sdot;;
                    newContentChars[newContentCharsPos] = '&';
                    newContentChars[newContentCharsPos + 1] = 's';
                    newContentChars[newContentCharsPos + 2] = 'd';
                    newContentChars[newContentCharsPos + 3] = 'o';
                    newContentChars[newContentCharsPos + 4] = 't';
                    newContentChars[newContentCharsPos + 5] = ';';
                    newContentCharsPos += 6;
                    continue;
                case '<':
                    //< -> &lt;
                    newContentChars[newContentCharsPos] = '&';
                    newContentChars[newContentCharsPos + 1] = 'l';
                    newContentChars[newContentCharsPos + 2] = 't';
                    newContentChars[newContentCharsPos + 3] = ';';
                    newContentCharsPos += 4;
                    continue;
                case '>':
                    //> -> &gt;
                    newContentChars[newContentCharsPos] = '&';
                    newContentChars[newContentCharsPos + 1] = 'g';
                    newContentChars[newContentCharsPos + 2] = 't';
                    newContentChars[newContentCharsPos + 3] = ';';
                    newContentCharsPos += 4;
                    continue;
                default:
                    newContentChars[newContentCharsPos] = originContentChar;
                    newContentCharsPos++;
            }
        }
        return new String(newContentChars);
    }

但是,其他的都没问题,就间隔号有问题,依旧解析不了,查了一下,间隔号有对应,&middot;,&sdot;但是都不起作用,后来想了想是不是flt文件,也就是对应的html就有问题,于是打开看了一下,果然,自己的ftl文件缺少html的doctype标识,将<html>改成如下,问题得以解决

<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值