Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HS {
- public String readxml() throws ParserConfigurationException, SAXException, IOException { // reads the xml file for highscores and then outputs them as a string
- try {
- String temp = "";
- String tempscore = "";
- String tempname = "";
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.parse(new File("highscores.xml"));
- doc.getDocumentElement().normalize();
- System.out.println("Original File: "+ doc.getDocumentElement().getNodeName()); //retrieve
- NodeList listOfScores = doc.getElementsByTagName("name"); // parent name
- int scoretotal = listOfScores.getLength();
- System.out.println("Number of scores in the file: "+ scoretotal);// number of scores
- for (int s = 0; s<listOfScores.getLength(); s++) { //Searches the XML file for courses and then adds them to the node
- Node firstScoreNode = listOfScores.item(s);
- if (firstScoreNode.getNodeType() == Node.ELEMENT_NODE) {//if the nodes are the same...
- Element firstScoreElement = (Element) firstScoreNode;
- // below code retrieves each element,
- NodeList firstNameList = firstScoreElement.getElementsByTagName("name"); //prints the name
- Element firstNameElement = (Element) firstNameList.item(0);
- NodeList textFNList = firstNameElement.getChildNodes();
- tempname = tempname + ("Name: " + ((Node)textFNList.item(0)).getNodeValue().trim() + "\n");
- System.out.println(tempname);
- NodeList ScoreList = firstScoreElement.getElementsByTagName("score"); //prints the score
- Element ScoreElement = (Element) firstNameList.item(0);
- NodeList ScoresList = firstNameElement.getChildNodes();
- tempscore = tempscore + ("Score " + ((Node)textFNList.item(0)).getNodeValue().trim()+ "\n");
- System.out.println(tempscore);
- }
- temp = tempscore + tempname;
- System.out.println(temp);
- }
- return temp;
- } catch (SAXParseException err) {
- return"";
- }
- }
- public Rectangle backButton = new Rectangle(GameFrame.WIDTH / 2 - 150, 700, 300, 125);
- // this creates the code for the XML highscore section, hopefully each highscore gets put onto a new line
- public void render(Graphics g) throws ParserConfigurationException, SAXException, IOException {
- String gang = readxml();
- Graphics2D g2D = (Graphics2D) g;
- Font f0 = new Font ("arial", Font.BOLD, 50);
- Font f1 = new Font ("arial", Font.PLAIN, 30);
- g.setFont(f0);
- g.setColor(Color.WHITE);
- g.drawString("Highscores", GameFrame.WIDTH / 2 - 150, 100);
- g.drawString("Back", GameFrame.WIDTH / 2 - 65, 780);
- g.setFont(f1);
- g.drawString(gang, GameFrame.WIDTH / 2 - 180, 350);
- g2D.draw(backButton);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement