使用jsoup解析XML - 防止jsoup“清理” < LINK>标签 [英] Use jsoup to parse XML - prevent jsoup from "cleaning" <link> tags

查看:180
本文介绍了使用jsoup解析XML - 防止jsoup“清理” < LINK>标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数情况下,使用jsoup解析XML没有问题。但是,如果XML文档中有< link> 标记,jsoup将在此处更改< link>一些文字< / link> < link />这里的一些文字。这使得无法使用CSS选择器在< link> 标记内提取文本。

In most case, I have no problem with using jsoup to parse XML. However, if there are <link> tags in the XML document, jsoup will change <link>some text here</link> to <link />some text here. This makes it impossible to extract text inside the <link> tag using CSS selector.

那么如何预防jsoup来自清洁< link> 标签?

So how to prevent jsoup from "cleaning" <link> tags?

推荐答案

In jsoup 1.6.2 我添加了一个XML解析器模式,它按原样解析输入,不应用HTML5解析规则(元素的内容,文档结构等)。此模式会将文字保存在< 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). This mode will keep text in a <link> tag, and allow multiples of it, etc.

这是例如:

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());

退货:

Link text 1: One
Link text 2: Two

这篇关于使用jsoup解析XML - 防止jsoup“清理” &LT; LINK&GT;标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆