java - JSoup 从 xml 中剥离 html 标记

标签 java html jsoup

我一直在寻找 stackoverflow,但找不到遇到此类问题的人。

我想做这样的事情:

输入字符串:

<?xml version="1.0" encoding="UTF-8" ?>
<List>
  <Object>
    <Section>Fruit</Section>
    <Category>Bananas</Category>
    <Brand>Chiquita</Brand>
    <Obs><p>
Vende-se a pe&ccedil;as ou o conjunto.</p><br>
    </Obs>
  </Object>
</List>

我想要的是去掉 html 标签,比如 <p>,<br>等等 所以它像这样结束:

<?xml version="1.0" encoding="UTF-8" ?>
<List>
  <Object>
    <Section>Fruit</Section>
    <Category>Bananas</Category>
    <Brand>Chiquita</Brand>
    <Obs>
Vende-se a pe&ccedil;as ou o conjunto.
    </Obs>
  </Object>
</List>

我一直在玩弄 JSoup,但我似乎无法让它正常工作。

这是我的代码:

Whitelist whitelist = Whitelist.none();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><List><Object><Section>Fruit</Section><Category>Bananas</Category><Brand>Chiquita</Brand><Obs><p>Vende-se a pe&ccedil;as ou o conjunto.</p><br></Obs></Object></List>";

whitelist.addTags(new String[]{"?xml", "List", "Object", "Section", "Category", "Brand", "Obs"});
String safe = Jsoup.clean(xml, whitelist);

这是我得到的结果:

FruitBananasChiquitaVende-se a pe&ccedil;as ou o conjunto.

提前致谢

最佳答案

标签是小写的,使用:

whitelist.addTags(new String[] { "?xml", "list", "object", "section",
    "category", "brand", "obs" });

输出:

<list>
 <object>
  <section>
   Fruit
  </section>
  <category>
   Bananas
  </category>
  <brand>
   Chiquita
  </brand>
  <obs>
   Vende-se a pe&ccedil;as ou o conjunto.
  </obs></object>
</list>

关于java - JSoup 从 xml 中剥离 html 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21833738/

相关文章:

html - 使用静态网站防止缓存和过时的内容

java - Android Java JSoup 搜索然后检索结果

java - Jsoup .data() 内部循环

java - Playframework - Eclipse 无法检测到新模板

html - 如何将元素的大小调整到完整的可见高度

Java: mytextarea.setText ("hello") + Thread.sleep(1000) = 奇怪的结果

html - 在嵌入式 Jetty 上使用 DefaultServlet 提供静态 html 文件

java - 尝试访问 Login 元素,但没有元素 id 或类

java - TimerTask() 没有做应该做的事情,只打印一次

java - 子表上的 Hibernate Criteria API