【发布时间】:2016-10-09 09:19:17
【问题描述】:
我有一个包含一些 HTML 标记的 XML 文件。我想保留 XML 标签但删除 HTML 标签。例如在下面的结构中
<xml_tag_parent>
<xml_tag_child>
Some text here <p> some parag here </p>
</xml_tag_child>
</xml_tag_parent>
我想得到:
<xml_tag_parent>
<xml_tag_child>
Some text here some parag here
</xml_tag_child>
</xml_tag_parent>
我事先不知道xml标签是什么。 另请注意,HTML 标记可能是嵌套的,因此我不能只获取节点的值。例如在以下 xml 文档中:
<description id="description">
<heading id="h-0001" level="1">CROSS REFERENCE</heading>
<p id="p-0002" num="0001">The Paragraph </p>
<claim attr="someAttr"> abcs </claim>
<claim attr="2">
<p> this is another paragraph <b>with some bold things</b> </p>
</claim>
</description id="description">
我想得到:
<description id="description">
CROSS REFERENCE The Paragraph
<claim attr="someAttr"> abcs </claim>
<claim attr="2">
this is another paragraph with some bold things
</claim>
</description id="description">
我可以尝试对所有 HTML 标记进行硬编码,找到它们并删除它们。例如,我可以查找 标签并将其替换为空字符串,但这听起来不正确,此外还有很多标签需要覆盖。是否有 Java 中的库或更好的方法来做到这一点?
【问题讨论】: