我收到了一些这样的 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>
如您所见,对于相同的值,我有超过 1 个标签,而每个文本只需要 1 个标签:
<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;
}