本文介绍了JSoup.clean()不保留相对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过:

Whitelist.relaxed();
Whitelist.relaxed().preserveRelativeLinks(true);
Whitelist.relaxed().addProtocols("a","href","#","/","http","https","mailto","ftp");
Whitelist.relaxed().addProtocols("a","href","#","/","http","https","mailto","ftp").preserveRelativeLinks(true);

它们都不起作用:当我尝试清理相对URL时,例如<a href="/test.xhtml">test</a>,我删除了href属性(<a>test</a>).

None of them work: When I try to clean a relative url, like <a href="/test.xhtml">test</a> I get the href attribute removed (<a>test</a>).

我正在使用JSoup 1.8.2.

I am using JSoup 1.8.2.

有什么想法吗?

推荐答案

问题很可能源于clean方法的调用.如果您提供基本URI,则所有功能都应按预期工作:

The problem most likely stems from the call of the clean method. If you give the base URI all should work as expected:

String html = ""
        + "<a href=\"/test.xhtml\">test</a>"
        + "<invalid>stuff</invalid>"
        + "<h2>header1</h2>";
String cleaned = Jsoup.clean(html, "http://base.uri", Whitelist.relaxed().preserveRelativeLinks(true));
System.out.println(cleaned);

以上内容有效并保留了相关链接.对于String cleaned = Jsoup.clean(html, Whitelist.relaxed().preserveRelativeLinks(true)),该链接将被删除.

The above works and keeps the relative links. With String cleaned = Jsoup.clean(html, Whitelist.relaxed().preserveRelativeLinks(true)) however the link is deleted.

请注意 Whitelist.preserveRelativeLinks(true)的文档:

这篇关于JSoup.clean()不保留相对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:19