【问题标题】:Reading/Displaying information from an XML File On Swing [duplicate]从 XML 文件中读取/显示信息 On Swing [重复]
【发布时间】:2014-06-17 01:33:30
【问题描述】:

我正在尝试记录一个人的姓名、分数等。

当我尝试编译时出现大量错误?也许我只是错过了一些东西,但它看起来对我来说是正确的,请帮忙。非常感谢!

        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) {

    }
}

这是 score.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)

【问题讨论】:

  • 发布分数 xml...
  • 消息告诉您确切地出了什么问题。我会批判性地阅读它,并修复它告诉你修复的内容。您需要在错误所在的站点捕获或抛出 ParserConfigurationException。再说一次,你不应该忽略你的 catch 块。在尝试编写更多代码之前,请阅读有关异常的教程部分。这将是美好的时光。
  • 开始here
  • 你读过错误信息吗?
  • 另外,您可能想学习 XPath,它会让您的生活更轻松;)

标签: java swing


【解决方案1】:

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

您在iffor 之后缺少结束}...

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+,则可以使用上面演示的 multi-catch,否则,您需要单独捕获每个...

您可能需要一些时间阅读Exceptions trail 以了解更多信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多