capitals.xml XML Reader


/ Published in: Java
Save to your folder(s)

Stores country name code and capital in an array and returns it. (not completed)


Copy this code and paste it in your HTML
  1. package xmlReader;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FilePermission;
  7. import java.io.FileReader;
  8. import java.io.IOException;
  9. import java.nio.file.Files;
  10. import java.util.StringTokenizer;
  11.  
  12. public class CapitalsReader {
  13.  
  14. String fileName;
  15.  
  16. String [][] capitals = new String[249][3];
  17.  
  18. int quoteCount = 0;
  19.  
  20. public CapitalsReader()
  21. {
  22.  
  23.  
  24. }
  25.  
  26. public static void main(String[] args) {
  27.  
  28. CapitalsReader capitals_reader = new CapitalsReader();
  29.  
  30. capitals_reader.Read();
  31.  
  32. System.out.println("End");
  33.  
  34. }
  35.  
  36. public String [][] Read()
  37. {
  38.  
  39. try {
  40.  
  41. String fileName ="C:/Users/User/workspace/se5/src/xmlReader/Capitals.xml";
  42. BufferedReader br = new BufferedReader( new FileReader(fileName));
  43.  
  44. int lineNumber = 0; int tokenNumber = 0;
  45.  
  46. while( (fileName = br.readLine()) != null)
  47. {
  48.  
  49. lineNumber++;
  50.  
  51. tokenNumber = 0;
  52.  
  53. // skip second line
  54.  
  55. if(lineNumber!=1)
  56.  
  57. {
  58.  
  59. StringTokenizer stringTokenizer = new StringTokenizer(fileName);
  60.  
  61. while (stringTokenizer.hasMoreTokens())
  62.  
  63. {
  64.  
  65. String token = stringTokenizer.nextToken();
  66.  
  67. if(tokenNumber == 0 && lineNumber ==4)
  68.  
  69. {
  70. System.out.println("tokenNumber: "+tokenNumber);
  71. System.out.println("token: "+token);
  72. }
  73.  
  74.  
  75.  
  76. tokenNumber++;
  77.  
  78. /*
  79. TO DO code to check for the tokens you want so you
  80. can insert the correct elements into the String [][]
  81. capitals. Use the fact that the country name begins
  82. after the first " and ends at the second, the country
  83. code runs from the fifth " to the 6th and the capital
  84. from the 15th " to the 16th. Once you've processed the
  85. capital, break ou of the while loop.
  86. */
  87.  
  88. //if()
  89.  
  90. break;
  91. }
  92.  
  93. lineNumber++;
  94.  
  95. }
  96.  
  97. }
  98.  
  99. } catch (FileNotFoundException e) {
  100. // TODO Auto-generated catch block
  101. e.printStackTrace();
  102. } catch (IOException e) {
  103. // TODO Auto-generated catch block
  104. e.printStackTrace();
  105. }
  106.  
  107. finally
  108. {
  109. // do whatever you want here i.e. print something like "finished"
  110. }
  111. return capitals;
  112.  
  113.  
  114. }
  115.  
  116. }

URL: https://github.com/mledoze/countries/blob/master/countries.xml

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.