Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. public class HS {
  2. public String readxml() throws ParserConfigurationException, SAXException, IOException { // reads the xml file for highscores and then outputs them as a string
  3.  
  4. try {
  5. String temp = "";
  6. String tempscore = "";
  7. String tempname = "";
  8.  
  9. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  10. DocumentBuilder db = dbf.newDocumentBuilder();
  11. Document doc = db.parse(new File("highscores.xml"));
  12. doc.getDocumentElement().normalize();
  13. System.out.println("Original File: "+ doc.getDocumentElement().getNodeName()); //retrieve
  14. NodeList listOfScores = doc.getElementsByTagName("name"); // parent name
  15. int scoretotal = listOfScores.getLength();
  16. System.out.println("Number of scores in the file: "+ scoretotal);// number of scores
  17.  
  18. for (int s = 0; s<listOfScores.getLength(); s++) { //Searches the XML file for courses and then adds them to the node
  19.  
  20. Node firstScoreNode = listOfScores.item(s);
  21.  
  22. if (firstScoreNode.getNodeType() == Node.ELEMENT_NODE) {//if the nodes are the same...
  23.  
  24. Element firstScoreElement = (Element) firstScoreNode;
  25. // below code retrieves each element,
  26. NodeList firstNameList = firstScoreElement.getElementsByTagName("name"); //prints the name
  27. Element firstNameElement = (Element) firstNameList.item(0);
  28. NodeList textFNList = firstNameElement.getChildNodes();
  29. tempname = tempname + ("Name: " + ((Node)textFNList.item(0)).getNodeValue().trim() + "\n");
  30. System.out.println(tempname);
  31.  
  32. NodeList ScoreList = firstScoreElement.getElementsByTagName("score"); //prints the score
  33. Element ScoreElement = (Element) firstNameList.item(0);
  34. NodeList ScoresList = firstNameElement.getChildNodes();
  35. tempscore = tempscore + ("Score " + ((Node)textFNList.item(0)).getNodeValue().trim()+ "\n");
  36. System.out.println(tempscore);
  37.  
  38.  
  39. }
  40. temp = tempscore + tempname;
  41. System.out.println(temp);
  42. }
  43.  
  44. return temp;
  45. } catch (SAXParseException err) {
  46. return"";
  47. }
  48.  
  49. }
  50.  
  51.  
  52.  
  53. public Rectangle backButton = new Rectangle(GameFrame.WIDTH / 2 - 150, 700, 300, 125);
  54.  
  55.  
  56. // this creates the code for the XML highscore section, hopefully each highscore gets put onto a new line
  57. public void render(Graphics g) throws ParserConfigurationException, SAXException, IOException {
  58. String gang = readxml();
  59.  
  60. Graphics2D g2D = (Graphics2D) g;
  61. Font f0 = new Font ("arial", Font.BOLD, 50);
  62. Font f1 = new Font ("arial", Font.PLAIN, 30);
  63. g.setFont(f0);
  64. g.setColor(Color.WHITE);
  65. g.drawString("Highscores", GameFrame.WIDTH / 2 - 150, 100);
  66. g.drawString("Back", GameFrame.WIDTH / 2 - 65, 780);
  67. g.setFont(f1);
  68. g.drawString(gang, GameFrame.WIDTH / 2 - 180, 350);
  69.  
  70.  
  71. g2D.draw(backButton);
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement