在Swing上从XML文件读取/显示信息

时间:2014-06-17 01:33:30

标签: java swing

我正在努力记录一个人的姓名,得分等。

我尝试编译时遇到大量错误?也许我只是错过了一些东西,但看起来对我来说,请帮助。非常感谢你!

        private void scoreButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            

    try { 
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new File("scores.xml"));
        doc.getDocumentElement().normalize();
        System.out.println("Root element of the doc is "+ doc.getDocumentElement().getNodeName());
        NodeList listOfPlayers = doc.getElementsByTagName("player");
        int totalPersons = listOfPlayers.getLength();
        System.out.println("Total number of people: "+ totalPersons);



    for (int s = 0; s<listOfPlayers.getLength(); s++) {
            Node firstPersonNode = listOfPlayers.item(s);
          if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
                Element firstPersonElement = (Element) firstPersonNode;
                NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                Element firstNameElement = (Element) firstNameList.item(0);
                NodeList textFNList = firstNameElement.getChildNodes();
                System.out.println("First Name: " + ((Node)textFNList.item(0)).getNodeValue().trim());
                NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
                Element lastNameElement = (Element)lastNameList.item(0);
                NodeList textLNList = lastNameElement.getChildNodes();
                System.out.println("Money: " + ((Node)textLNList.item(0)).getNodeValue().trim());
                NodeList ageList = firstPersonElement.getElementsByTagName("age");
                Element ageElement = (Element)ageList.item(0);
                NodeList textAgeList = ageElement.getChildNodes();
                System.out.println("Age: " + ((Node)textAgeList.item(0)).getNodeValue().trim());

            }
        } catch(SAXParseException err) {

    }
}

这是scores.xml文件:

    <scores>

<player>
    <name> Sam Moeza </name>
    <money> 100 </money>
    <age> 22 </age>
</player>

<player>
    <name> Larry Hoover </name>
    <money> 100000 </money>
    <age> 55 </age>
</player>

    </scores>

这是错误:

    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - unreported exception javax.xml.parsers.ParserConfigurationException; must be caught or declared to be thrown
    at casinoroulette.CasinoRouletteView.scoreButtonActionPerformed(CasinoRouletteView.java:327)
    at casinoroulette.CasinoRouletteView.access$1200(CasinoRouletteView.java:24)
    at casinoroulette.CasinoRouletteView$8.actionPerformed(CasinoRouletteView.java:173)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at    java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

1 个答案:

答案 0 :(得分:2)

让我们从显而易见的......

开始

您在}if之后错过了结束for ...

try { 
    //...
    for (int s = 0; s<listOfPlayers.getLength(); s++) {
        Node firstPersonNode = listOfPlayers.item(s);
        if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
            //...
        //.. nothing here?
    }
} catch(SAXParseException err) {

}

然后转到你需要扩展try-catch来处理javax.xml.parsers.ParserConfigurationExceptionjava.io.IOException

的事实

例如......

try { 
    //...
    for (int s = 0; s<listOfPlayers.getLength(); s++) {
        Node firstPersonNode = listOfPlayers.item(s);
        if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
            //...
        } // Fix me
    }
} catch (ParserConfigurationException | SAXException | IOException ex) {
    // Don't forget to do something with your exception, like
    // log it or show and error message dialog
    ex.printStackTrace();
}

如果您使用的是Java 7+,您可以使用上面演示的多捕获,否则,您需要单独捕获每个捕获...

您可能需要花一些时间阅读Exceptions trail以获取更多信息