Netbeans(Java) - 将XML节点存储在整数数组中

时间:2016-06-03 02:35:38

标签: java xml type-conversion xmlnode

我正在为学校项目创建一个简单的高分页面。我需要使用xml文件来存储数据。我计划创建一个在比赛结束时保留得分的高分页面如下:

    // Variable Declaration
    int[] highscoresArray = new int [12];
    String[] usernameArray = new String [12];
    // score temp and username temp variables.
    int sTemp;
    String uTemp;

    // Prevent blank username
    if (nameInput.getText().length() > 2){
        warnings.setText("Username may only be 2 digits!");
    }
    else if(nameInput.getText() != null && nameInput.getText().length() == 2) {
        try {
            // Gets the contents from highscores.xml & assigns each Node to an int variable;
            String filePath = "highscores.xml";
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(filePath);

            // Read username data from xml file
            for (int y = 0; y <= 9; y++){
                Node uName = doc.getElementsByTagName("name").item(y);
                usernameArray[y] = uName.getTextContent();

                System.out.println("User " + y + ". " + usernameArray[y]);
            }
            usernameArray[10] = nameInput.getText();
            System.out.println("User 10. " + usernameArray[10]);

            // Read userscore data from xml file
            for (int y = 0; y < 9; y++){
                Node uScore = doc.getElementsByTagName("score").item(y);
                highscoresArray[y] = Integer.parseInt(uScore.getTextContent());

                System.out.println("Score " + y + ". " + highscoresArray[y]);
            }
            highscoresArray[10] = gameUI.bankOffer;
            System.out.println("Score 10. " + highscoresArray[10]);


            for (int x = 10; x >= 1; x--){
                if (highscoresArray[x] > highscoresArray[x-1]) {
                    // score swap
                    sTemp = highscoresArray[x-1];
                    highscoresArray[x-1] = highscoresArray[x];
                    highscoresArray[x] = sTemp;

                    // username swap
                    uTemp = usernameArray[x-1];
                    usernameArray[x-1] = usernameArray[x];
                    usernameArray[x] = uTemp;
                }
            }

        }catch (ParserConfigurationException pce){
            pce.printStackTrace();
        }catch(IOException ioe){
            ioe.printStackTrace();
        }catch(SAXException sae){
            sae.printStackTrace();
        }    
    } else {
        warnings.setText("Invalid username");
    }

我的程序在for循环中有问题,它从xml文件中读取用户分数。再次列出如下:

    // Read userscore data from xml file
    for (int y = 0; y < 9; y++){
        Node uScore = doc.getElementsByTagName("score").item(y);
        highscoresArray[y] = Integer.parseInt(uScore.getTextContent());

        System.out.println("Score " + y + ". " + highscoresArray[y]);
    }

我知道它与将Node值存储为int有关,但我尝试按照步骤进行转换。非常感谢任何帮助。

0 个答案:

没有答案