0

我试图找出一种方法来解析带有自定义标签的 html 文件,格式如下:

[custom tag="id"]

这是我正在使用的文件的示例:

<p>This is an <em>amazing</em> example. </p>
<p>Such amazement, <span>many wow.</span> </p>
<p>Oh look, a wild [custom tag="amaze"] appears.</p>
We need maor embeds <a href="http://youtu.be/F5nLu232KRo"> bro

我想要(在理想的世界中)返回的是元素列表):

List foundElements = [text, custom tag, text, link, text]

上面列表中的元素包含:

文本:

<p>This is an <em>amazing</em> example. </p>
<p>Such amazement, <span>many wow.</span> </p>
<p>Oh look, a wild [custom tag="amaze"] appears.</p>
We need maor embeds

自定义标签:

[custom tag="amaze"]

关联:

<a href="http://youtu.be/F5nLu232KRo">

文本:

 appears.</p>We need maor embeds

我试过的:

  1. Jsoup
    Jsoup 很棒,它非常适合 HTML。问题是我无法定义带有打开“[”和关闭“]”的自定义标签。如我错了请纠正我?
  2. Jericho
    和 Jsoup 一样,Jericho 工作得很好……除了定义自定义标签。您需要使用“<”。
  3. Java Regex
    这是我真的不想选择的选项。它不可靠,并且存在很多脆弱的字符串操作,尤其是当您与很多正则表达式匹配时。

最后但同样重要的是,我正在寻找一种以性能为导向的解决方案,因为这是在 Android 客户端上完成的。

欢迎所有建议!

4

0 回答 0