使用Jsoup解析标签内部标签时不用替换的自动方法
我正在阅读存储在description
标签上的RSS标签的HTML代码,它不是纯文本格式。我需要提取一些信息,比如第一张图片会出现,但是我不能这样做,因为description
里面的所有标签都没有被Jsoup解析,我想通过CDATA元素的行为来解析它。使用Jsoup解析标签内部标签时不用替换的自动方法
在我的问题我引荐到“自动方式” ,因为我看到在这里发表,我会用.replace()
删除CDATA,但它似乎不是我没有一个有效的解决方案,我认为这将有助于特定情况下的其他问题,不适用于通用目的。所以我的问题是,如果有一种方法可以让Jsoup进行解析而不会取代我的文本?这是唯一存在的方式吗?我应该使用其他库?
例如,当我分析了RSS文件,节点描述了这一点:
<table width='100%' border='0' cellspacing='0' cellpadding='4'><tr><td align='left' width='10'><
a href='http://www.3djuegos.com/noticia/145062/0/bioware-nuevo-juego-ip/video-gamescom/trailer/'><img src='http://i11c.3djuegos.com/juegos/7332/dragon_age_iii/fotos/noticias/dragon_age_iii-2583054.jpg' border='0' width='70' height='52' />
</a></td><td align='left' valign='top'>Parece ser una nueva licencia creativa, según lo visto en un enigm&aacu
所有特殊字符 “<>” 的scaped因为CDATA工作如此。文档的其余部分是良好解析只发生在CDATA内容中。
,我用它来接入代码:
doc = Jsoup.connect("http://www.3djuegos.com/universo/rss/rss.php?plats=1-2-3-4-5-6-7-34&tipos=noticia-analisis-avance-video-imagenes-demo&fotos=peques&limit=20").get();
System.out.println(doc.html()); // Shows the document well parsed.
Elements nodes = doc.getElementsByTag("item"); // Access to news
for(int i = 0; i < nodes.size(); i++){ // Loop all news
// Description node
Element decriptionNode = nodes.get(i).getElementsByTag("description").get(0);
// Shows content of node. Here is where HTML tags are escaped
System.out.println(nodes.get(i).getElementsByTag("description").html()); // Here prints the content of description tag and all HTML tags are escaped by default
// Access to first image and here fails because of description text is escaped
// and then Jsoup cant parsed as nodes
Element imageNode = descriptionNode.getElementsByTag("img").get(0);
}
编辑:我用doc.outputSettings().escapeMode(EscapeMode.xhtml)
但我想这不会影响到CDATA内容。
编辑2:我用作解决方法库org.apache.commons.lang3.StringEscapeUtils
,让unescape html,但我仍在考虑如果Jsoup已经有这方面的东西。
您可以使用text()
方法获取非转义值。这意味着如果某个元素的值为<table width='100%' border='0' cellspacing='0' cellpadding='4'>
,那么当您执行element.text()
时,它将返回<table width='100%' border='0' cellspacing='0' cellpadding='4'>
。所以你可以再次解析这个片段,从中得到你想要的任何东西。例如。
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class Sample {
public static void main(String[] args) throws Exception {
String html = "<description>"
+ "<table width='100%' border='0' cellspacing='0' cellpadding='4'><tr><td align='left' width='10'><"
+ "a href='http://www.3djuegos.com/noticia/145062/0/bioware-nuevo-juego-ip/video-gamescom/trailer/'><img src='http://i11c.3djuegos.com/juegos/7332/dragon_age_iii/fotos/noticias/dragon_age_iii-2583054.jpg' border='0' width='70' height='52' />"
+ "</a></td><td align='left' valign='top'>Parece ser una nueva licencia creativa, según lo visto en un enigm&aacu"
+ "</description>";
Document doc = Jsoup.parse(html);
for(Element desc : doc.select("description")){
String unescapedHtml = desc.text();
String src = Jsoup.parse(unescapedHtml).select("img").first().attr("src");
System.out.println(src);
}
System.out.println("Done");
}
}
非常感谢!我总是使用'html()'操作,我没有想到用'text()'全部逃脱都行!我会给你投票,但我没有足够的声望。再次感谢! – korima
这是一个不错的解决方案! +1 –
你必须提供一些你想要解析的样例代码,以及你想从中提取的东西! –
更新为CDATA问题。 – korima
你的Jsoup代码是什么? –