2013-06-13 4 views
0

나는 다음과 같은 몇 가지 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> 

당신이, 내가 같은 값에 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을 얻기 위해 올바르게 바꿀 수는 없다. 타이!

이 내 솔루션입니다 (나는 내 ​​자신의 질문을 anwser 수 없습니다, 그래서 내가 대신 여기 쓰기 : 나는 그것이 최선하지 않고 더 나은 수행 할 수 있습니다 알고

을, 그것을 작동합니다.

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;       
} 
+0

당신이 XPath는 솔루션를 시도 더 잘 할 수있어? http://stackoverflow.com/questions/862239/xpath-get-node-with-no-child-of-specific-type –

답변

0

마지막으로 나는 방법을 발견, 나는 그것이 최선하지 않고

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;       
}