Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. package game.src.main;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Rectangle;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.util.*;
  11.  
  12. import javax.xml.parsers.DocumentBuilder;
  13. import javax.xml.parsers.DocumentBuilderFactory;
  14. import javax.xml.parsers.ParserConfigurationException;
  15.  
  16. import org.w3c.dom.Document;
  17. import org.w3c.dom.Element;
  18. import org.w3c.dom.Node;
  19. import org.w3c.dom.NodeList;
  20. import org.xml.sax.SAXException;
  21. import org.xml.sax.SAXParseException;
  22.  
  23.  
  24. public class HS {
  25. public String readxml() throws ParserConfigurationException, SAXException, IOException { // reads the xml file for highscores and then outputs them as a string
  26.  
  27. try {
  28. String temp = "";
  29. String tempscore = "";
  30. String tempname = "";
  31.  
  32. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  33. DocumentBuilder db = dbf.newDocumentBuilder();
  34. Document doc = db.parse(new File("highscores.xml"));
  35. doc.getDocumentElement().normalize();
  36. System.out.println("Original File: "+ doc.getDocumentElement().getNodeName()); //retrieve
  37. NodeList listOfScores = doc.getElementsByTagName("name"); // parent name
  38. int scoretotal = listOfScores.getLength();
  39. System.out.println("Number of scores in the file: "+ scoretotal);// number of scores
  40.  
  41. for (int s = 0; s<listOfScores.getLength(); s++) { //Searches the XML file for courses and then adds them to the node
  42.  
  43. Node firstScoreNode = listOfScores.item(s);
  44.  
  45. if (firstScoreNode.getNodeType() == Node.ELEMENT_NODE) {//if the nodes are the same...
  46.  
  47. Element firstScoreElement = (Element) firstScoreNode;
  48. // below code retrieves each element,
  49. NodeList firstNameList = firstScoreElement.getElementsByTagName("name"); //prints the name
  50. Element firstNameElement = (Element) firstNameList.item(0);
  51. NodeList textFNList = firstNameElement.getChildNodes();
  52. tempname = tempname + ("Name: " + ((Node)textFNList.item(0)).getNodeValue().trim() + "\n");
  53. System.out.println(tempname);
  54.  
  55. NodeList ScoreList = firstScoreElement.getElementsByTagName("score"); //prints the score
  56. Elem
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement