本文介绍了Jsoup将HTML属性的输出从单引号更改为双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Jsoup解析,操纵和扩展html模板.到目前为止,一切正常,直到将单引号与HTML属性结合使用为止

We are using Jsoup to parse, manipulate and extend a html template. So far everything works fine until it comes to single quotes used in combination with HTML attributes

<span data-attr='JSON'></span>

该HTML代码段已转换为

That HTML snippet is converted to

<span data-attr="JSON"></span>

将与内部json数据冲突,该内部json数据仅用双引号指定为有效

which will conflict with the inner json data which is specified as valid with double quotes only

{"param" : "value"} //valid
{'param' : 'value'} //invalid

因此我们需要强制Jsoup 将那些单引号更改为双引号,但是如何?目前,这是我们用于解析和产生html内容的代码.

so we need to force Jsoup to NOT change those single quotes to double quotes, but how? Currently that is our code to parse and produce html content.

pageTemplate = Jsoup.parse(new File(mainTemplateFilePath), "UTF-8");
pageTemplate.outputSettings().escapeMode(Entities.EscapeMode.xhtml);
pageTemplate.outputSettings().charset("UTF-8");

... adding some html

pageTemplate.html(); // will output the double quoted attributes :(

推荐答案

您需要先对HTML JSON值 进行HTML编码,然后再将其放入data-attr属性.完成此操作后,您应该得到以下结果:

You need to HTML encode the JSON value before putting it into the data-attr attribute. When you do so, you should end up with this:

<span data-attr="{&quot;param&quot;:&quot;value&quot;}"></span>

尽管看起来相当令人生畏,但实际上它是有效的HTML.当相应的JavaScript执行someSpan.getAttribute("data-attr")时,&quot;值将自动转换为"值,从而使您可以访问原始的有效JSON字符串.

Although that looks fairly daunting, it is actually valid HTML. When your corresponding JavaScript executes someSpan.getAttribute("data-attr"), the &quot; values will be transformed into " values automatically, giving you access to the original valid JSON string.

这篇关于Jsoup将HTML属性的输出从单引号更改为双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:26
查看更多