Html 获取 value 时 value 值中包含 英文 ",导致获取数据异常

Html 获取 value 代码如下

String urlCotent = "<test describe=\"hello\"Man\" />";
HtmlParser htmlParser = new HtmlParser(urlCotent);
String describe = htmlParser.getValue("describe");
 // 预期打印 hello"Man。
System.out.println("describe " + describe);
// 实际打印 hello

原因:html 通过 key 获取 value 时是以一对英文 "xxx",中间的内容作为值。

解决方案,将内容中出现的英文 " 进行转义 " --> &quot;,使用 webView显示 或者 Html 解析时会自动反转义,所以不需担心。如果 textView 直接当做文本显示时需要手动转义。

注意:不能用 \\\" z对字符进行转义,得到的结果会是 hello\【\\ --> \  \" --> "】。

其他常用 html 转义字符 

& 【&amp;】、< 【&lt;】、> 【&gt;】、" 【&quot;】

正确写法:

String urlCotent = "<test describe=\"hello\"Man\" />".replaceAll("\"", "&quot;");
HtmlParser htmlParser = new HtmlParser(urlCotent);
String describe = htmlParser.getValue("describe");
 // 预期打印 hello"Man。
System.out.println("describe " + describe);
// 实际打印 hello"Man

猜你喜欢

转载自blog.csdn.net/ff_hh/article/details/85007987
今日推荐