Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package game.src.main;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Rectangle;
- import java.io.File;
- import java.io.IOException;
- import java.util.*;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
- import org.xml.sax.SAXParseException;
- 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
- Elem
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement