【发布时间】:2013-09-20 12:08:51
【问题描述】:
我需要删除以“section”结尾的字符串中的 XML 标记。例如在下面的 XML 字符串中
<OldSection>
<sectionTitle>Sample Title</sectionTitle>
<label> Hello Label </label>
<heading>Hi </heading>
<NewSection>
<section>
<InteractionSection>
<sectionTitle>Section Title</sectionTitle>
<label> Hello </label>
<heading>Hi </heading>
<para>
...
...
</para>
</InteractionSection>
<section>
</NewSection>
</OldSection>
我想删除以部分结尾的标签,即<OldSection>, </OldSection>
,<NewSection></NewSection>, <InteractionSection>, </InteractionSection> 等。应该单独删除标签,而不是标签中的内容。
我尝试了以下代码但没有工作..
stringformat sf = new stringformat();
// REturns the xml string given as input
String s = sf.getString();
String f = s;
f = f.replaceAll("\\<*Section[^>].*?\\>", "");
请有任何建议。
【问题讨论】:
-
使用 SAX。不要尝试使用正则表达式来处理 XML。
-
你也可以使用 XPAth 库,运行 xml 并查找要删除的节点。也许我可以通过你和示例,让我查找。
-
试试这个
\\<.*Section> -
在这样的问题中,您应该指出究竟是什么不起作用。否则人们不得不猜测你的问题是什么。
标签: java xml regex string replace