[英]Use jsoup to parse XML - prevent jsoup from “cleaning” <link> tags
In most case, I have no problem with using jsoup to parse XML. 在大多数情况下,使用jsoup解析XML没有问题。 However, if there are
<link>
tags in the XML document, jsoup will change <link>some text here</link>
to <link />some text here
. 但是,如果XML文档中有
<link>
标记,jsoup会<link>some text here</link>
将<link>some text here</link>
更改为<link />some text here
。 This makes it impossible to extract text inside the <link>
tag using CSS selector. 这使得无法使用CSS选择器在
<link>
标记内提取文本。
So how to prevent jsoup from "cleaning" <link>
tags? 那么如何防止jsoup“清理”
<link>
标签?
In jsoup 1.6.2 I have added an XML parser mode, which parses the input as-is, without applying the HTML5 parse rules (contents of element, document structure, etc). 在jsoup 1.6.2中,我添加了一个XML解析器模式,它按原样解析输入,而不应用HTML5解析规则(元素,文档结构等的内容)。 This mode will keep text in a
<link>
tag, and allow multiples of it, etc. 此模式将文本保留在
<link>
标记中,并允许其多个等。
Here's an example: 这是一个例子:
String xml = "<link>One</link><link>Two</link>";
Document xmlDoc = Jsoup.parse(xml, "", Parser.xmlParser());
Elements links = xmlDoc.select("link");
System.out.println("Link text 1: " + links.get(0).text());
System.out.println("Link text 2: " + links.get(1).text());
Returns: 返回:
Link text 1: One
Link text 2: Two
Do not store any text inside <link>
element - it's invalid. 不要在
<link>
元素中存储任何文本 - 它是无效的。 If you need extra information, keep it inside HTML5 data-*
attributes. 如果您需要额外信息,请将其保留在HTML5
data-*
属性中。 I'm sure jsoup won't touch it. 我确定jsoup不会碰它。
<link rel="..." data-city="Warsaw" />
There can be a workaround for this. 可以有一个解决方法。 Before passing XML to jsoup.
在将XML传递给jsoup之前。 Transform XML file to replace all with some dummy tag say and do what you want to do.
转换XML文件以替换所有带有虚拟标记的文件并执行您想要执行的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.