2017-10-13 1 views
0

중복 된 항목에 대한 xml 파일을 확인해야하는 작은 Java 클래스를 작성해야합니다.XML 파일에서 Java로 중복 항목을 검사하려면 어떻게해야합니까?

XML 파일에는 독일어 단어와 영문 번역 키 값이 있습니다. 약 20,000 줄입니다.

예 : I 읽기/파일을 가져 여러 항목을 나중에이를 테스트 할 수 있습니다 방법

<properties> 
<entry key="Auto">car</entry> 
<entry key="Bus">bus</entry> 
<entry key="Auto">car</entry> 
<entry key="Haus">House</entry> 
</properties> 

. 내 코드는 모든 요소를 ​​찾지 만 올바른 순서는 찾지 않습니다.

이 파일을 읽는 내 코드입니다.

package translation; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.Enumeration; 
import java.util.InvalidPropertiesFormatException; 
import java.util.Properties; 

public class doubleTest { 

public static void main(String[] args) 
{ 

    try { 

     File file = new File("C://GER_EN.xml"); 
     FileInputStream fileInput = new FileInputStream(file); 
     Properties properties = new Properties(); 
     properties.loadFromXML(fileInput); 


     Enumeration enuKeys = properties.keys(); 
     while(enuKeys.hasMoreElements()) { 
      String key = (String) enuKeys.nextElement(); 
      String value = properties.getProperty(key); 
      System.out.println(key); //+ ": " + value 
     } 
     fileInput.close(); 

}catch (FileNotFoundException e) { 

    e.printStackTrace(); 

} catch (InvalidPropertiesFormatException e) { 

    e.printStackTrace(); 

} catch (IOException e) { 

    e.printStackTrace(); 

} 
} 

}

이것은 위의 XML 예에서는 출력한다.

Auto: car 
Bus: bus 
Haus: house 

감사

+0

: 사전에 많은 당신의 출력 –

+0

를 공유하시기 바랍니다 중복 키를 찾을 의미입니까? –

+0

"올바른 순서가 아니라"란 무엇을 의미합니까? –

답변

0
final Set<String> keySet = new HashSet<>(); 
for (final Enumeration<Object> keys = properties.keys(); 
    keys.hasMoreElements();) { 
    final String key = (String) keys.nextElement(); 
    if (keySet.contains(key)) { 
     // duplicate key! 
    } 
} 
+0

감사합니다. 지금 회의가 있습니다.하지만 나중에 바로 확인하겠습니다. – burschi

관련 문제