java删除XML重复标记
我收到的一些xml如下:
<cite id="0ac50429-bfbd-74e5-81bf-be29583cba3b">
<cite id="0ac50429-bfbd-74e5-81bf-be2a36aec2df">
<cite id="0ac50429-bfbd-74e5-81bf-be3d125bdc1c">Some Text
</cite>
</cite>
</cite>
<p>random text</p>
<cite id="0ac50429-bfbd-74e5-81bf-be29583cba3b">
<cite id="0ac50429-bfbd-74e5-81bf-be2a36aec2df">
<cite id="0ac50429-bfbd-74e5-81bf-be3d125bdc1c">
More text
</cite>
</cite>
</cite>
正如你所看到的,对于相同的值,我有不止一个标记,而我只需要每个文本有一个标记:
<cite id="0ac50429-bfbd-74e5-81bf-be3d125bdc1c">Some Text</cite>
<p>random text</p>
<cite id="0ac50429-bfbd-74e5-81bf-be29583cba3b">More text</cite>
但我找不到一个好办法来摆脱它。有人有线索吗?我试着要最后一个孩子,但我就是找不到。我试过使用正则表达式,我可以得到最后一个节点,但我不能正确地替换它们,以获得所需的xml。 泰
这是我的解决方案(我不能回答自己的问题,所以我写在这里:)
我知道这不是最好的,而且可以做得更好,这很有效
private static String replaceNodes(String simpleRegex, String xml)
{
String tagMultiple;
String expresionRegular = "("+simpleRegex+")+";
Pattern pattern = Pattern.compile(expresionRegular);
Matcher matcher = pattern.matcher(xml);
while(matcher.find()) // Here we look for all the nodes that are repeated . EJ <cite id="asda"><cite id="asda"><cite id="asda">
{
Pattern patternSimple = Pattern.compile(simpleRegex);
Matcher matcherSimple = patternSimple.matcher(xml);
String tagUnicoEnd ="";
if (matcherSimple.find()) //Here we get only one node. <cite id="asda">
tagUnicoEnd = matcher.group(1);
tagMultiple = matcher.group();
xml =xml.replace(tagMultiple,tagUnicoEnd); //we replace all the repetead nodes, with the unique one.
}
return xml;
}
# 1 楼答案
最后我找到了一个方法,我知道这不是最好的,可以做得更好