2017-10-13 7 views
0

重複するエントリのxmlファイルをチェックする必要がある小さなJavaクラスを作成する必要があります。Javaで重複するエントリのXMLファイルを確認するにはどうすればよいですか?

XMLファイルには、ドイツ語と英訳のキー値があります。それは約20,000行です。

例:私は読み取り/ファイルをインポートして、複数のエントリのために、後でそれらをテストするにはどうすればよい

<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

関連する問題