首页 \ 问答 \ 解析java中的dom错误(Parsing dom error in java)

解析java中的dom错误(Parsing dom error in java)

我试图解析XML,然后将其插入Excel文件。 如果我运行我的代码它甚至可以正常工作但我不能对它进行任何修改,因为我仍然有错误。 这是我的代码:

public class Parsing {
    private void parseXmlFile(){
        //get the factory
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        //using Factory get an instance of document builder
        DocumentBuilder db = dbf.newDocumentBuilder();

        //parse using builder to get DOM representation 
        dom = db.parse("Employee.xml"); }
    } catch )

    }
  }

这有什么问题? 有人能帮我吗? 我一直在搜索谷歌,它正在吃我的神经。


I am trying to parse an XML and then insert it an Excel File. If I run my code it works even with errors but I cannot make any modification to it because I still got errors. Here is my code:

public class Parsing {
    private void parseXmlFile(){
        //get the factory
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        //using Factory get an instance of document builder
        DocumentBuilder db = dbf.newDocumentBuilder();

        //parse using builder to get DOM representation 
        dom = db.parse("Employee.xml"); }
    } catch )

    }
  }

What is wrong with this? Can someone help me? I've been searching all over google and it's eating my nerves.


原文:https://stackoverflow.com/questions/31471711
更新时间:2022-11-25 12:11

最满意答案

在您的SecondUI您正在创建OneUI新实例

final OneUI obj = new OneUI();
obj.setVisible(true);// you don't call even this

但是你不称之为真正可见的。所以有一个隐藏的jframe和combobox组合框被更新,但不是第一帧的组合框,并且你看不到更新的组合框,因为框架不可见

解决这个传递给secondui的oneui引用然后调用那个引用的方法

例如OneUI.java

public class OneUI extends JFrame {

    private JPanel contentPane;
    private JComboBox comboBox;
    private DefaultComboBoxModel modeltest;
    private Integer count = 0;
    private JButton btnOpenSecondUi;
    private SecondUI secondui;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                OneUI frame = new OneUI();
                frame.setVisible(true);
            }
        });
    }

    public OneUI() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnAddOne = new JButton("Add 1 element");
        btnAddOne.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                updateModelCmb();
            }
        });
        btnAddOne.setBounds(187, 46, 129, 23);
        contentPane.add(btnAddOne);
        modeltest = new DefaultComboBoxModel();
        comboBox = new JComboBox();
        comboBox.setBounds(48, 47, 129, 20);
        comboBox.setModel(modeltest);
        contentPane.add(comboBox);

        btnOpenSecondUi = new JButton("Open second UI");
        btnOpenSecondUi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               secondui= new SecondUI(OneUI.this); // pass reference of this oneui  to secondui .so secondui can catch reference of this class[this frame] and update this combobox by calling updateModelCmb on this reference

            }
        });
        btnOpenSecondUi.setBounds(155, 163, 161, 23);
        contentPane.add(btnOpenSecondUi);
    }

    public void updateModelCmb() {
        count++;
        modeltest.addElement(count);
        comboBox.setModel(modeltest);

    }

}

SecondUI.java

public class SecondUI extends JDialog {

    private final JPanel contentPanel = new JPanel();

    public SecondUI(OneUI oneui) {
        //setVisible(true); // don't call setvisible here call setvisible after you add all the component .
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 327, 142);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setLayout(new FlowLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

        getContentPane().add(contentPanel, BorderLayout.CENTER);
        {
            JButton btnAddOneElement = new JButton("Add 1 element");
            btnAddOneElement.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    oneui.updateModelCmb();
                }
            });
            contentPanel.add(btnAddOneElement);
        }
        setVisible(true); // call setvisible at last
    }

}

in your SecondUI you are creating new instance of OneUI

final OneUI obj = new OneUI();
obj.setVisible(true);// you don't call even this

but you don't call it's visible to true.so there is a hidden jframe and combobox that combobox get updated but not first frame's combobox and you can't see updated combobox because the frame is not visible

to fix this pass oneui reference to secondui then call method of that reference

example OneUI.java

public class OneUI extends JFrame {

    private JPanel contentPane;
    private JComboBox comboBox;
    private DefaultComboBoxModel modeltest;
    private Integer count = 0;
    private JButton btnOpenSecondUi;
    private SecondUI secondui;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                OneUI frame = new OneUI();
                frame.setVisible(true);
            }
        });
    }

    public OneUI() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnAddOne = new JButton("Add 1 element");
        btnAddOne.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                updateModelCmb();
            }
        });
        btnAddOne.setBounds(187, 46, 129, 23);
        contentPane.add(btnAddOne);
        modeltest = new DefaultComboBoxModel();
        comboBox = new JComboBox();
        comboBox.setBounds(48, 47, 129, 20);
        comboBox.setModel(modeltest);
        contentPane.add(comboBox);

        btnOpenSecondUi = new JButton("Open second UI");
        btnOpenSecondUi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               secondui= new SecondUI(OneUI.this); // pass reference of this oneui  to secondui .so secondui can catch reference of this class[this frame] and update this combobox by calling updateModelCmb on this reference

            }
        });
        btnOpenSecondUi.setBounds(155, 163, 161, 23);
        contentPane.add(btnOpenSecondUi);
    }

    public void updateModelCmb() {
        count++;
        modeltest.addElement(count);
        comboBox.setModel(modeltest);

    }

}

SecondUI.java

public class SecondUI extends JDialog {

    private final JPanel contentPanel = new JPanel();

    public SecondUI(OneUI oneui) {
        //setVisible(true); // don't call setvisible here call setvisible after you add all the component .
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 327, 142);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setLayout(new FlowLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

        getContentPane().add(contentPanel, BorderLayout.CENTER);
        {
            JButton btnAddOneElement = new JButton("Add 1 element");
            btnAddOneElement.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    oneui.updateModelCmb();
                }
            });
            contentPanel.add(btnAddOneElement);
        }
        setVisible(true); // call setvisible at last
    }

}

相关问答

更多
  • 组合框接受任何类型的对象。 为了显示它们的值,它使用对象的toString()方法。 所以,你可能有这样的代码: @Entity public class Genre { // fields and methods @Override public String toString() { return this.name; } } // in your GUI List genres = findAllGenresSortedByName( ...
  • 你可以用组合框制作一个构造函数...... private JComboBox comboBox; //constructor public ClassAddItem(JComboBox box) { this.comboBox = box; .... } 并在actionListener中使用此comboBox public void actionPerformed(ActionEvent e) { this.comboBox.addItem("this does work"); ...
  • 首先一些关于MCVE的评论(因为你将在未来包括每个问题)。 我们希望代码可以放在单个源文件中,这样我们就可以轻松地复制/粘贴编译和测试。 我们不希望在我们的机器上放置3个文件,我们需要在测试后进行清理。 只应包括与问题直接相关的相关代码。 为什么你有TestTableModel类。 “列名”是否与问题相关? 要点总是在可能的情况下使用标准的JDK类来测试您的MCVE。 关于EventComboListener类。 再次,这可以通过使用和annoymouse内部类或lambda添加到组合框。 这将代码保存在一 ...
  • 在您的SecondUI您正在创建OneUI新实例 final OneUI obj = new OneUI(); obj.setVisible(true);// you don't call even this 但是你不称之为真正可见的。所以有一个隐藏的jframe和combobox组合框被更新,但不是第一帧的组合框,并且你看不到更新的组合框,因为框架不可见 解决这个传递给secondui的oneui引用然后调用那个引用的方法 例如OneUI.java public class OneUI extends ...
  • 您不应该调用setBounds() ,您需要使用布局来管理组件位置 You're not supposed to call setBounds(), you need to use a layout to manage the components positions
  • JDialog有另一个按钮,我想在点击它时打开另一个JFrmae。 不要那样做。 一个典型的Swing应用程序有一个主JFrame和几个JDialog 。 请参阅此主题使用多个JFrame,好/坏练习? 结果:另一个Jframe打开但它不会到达顶部。它显示在对话框下。我想在该对话框的顶部打开第二个JFrame。 当然这是因为对话框是模态的。 可以使用secondFrame.setAlwaysOnTop(true); 但我没有控制权来关闭或移动它。 它不会解决任何问题,因为问题与对话框中的模态有关。 请参阅此 ...
  • 唯一允许的所有者参数是顶级框架(我认为我不能从JPanel访问) 您可以使用以下方法访问面板的父框架: Window window = SwingUtilities.windowForComponent( yourPanelHere ); 然后只需使用该窗口作为对话框的所有者。 the only owner parameters that are allowed are top level Frames (which I don't think I can access from the JPanel Y ...
  • 代码是错误的,因为每次调用actionPerformed() ,您都在创建一个新组件: table = new JTable(new TestTableModel(data, header)); frame.add(new JScrollPane( table )); // <-- BTW: here you need to put the table otherwise you are adding an empty JScrollPane frame.validate(); (注意:还有一个bug, ...
  • JComboBox包含在CellEditor 。 您必须检索包装的组件,例如在使用DefaultCellEditor : DefaultCellEditor editor = (DefaultCellEditor)table.getColumnModel().getColumn(3).getCellEditor(); JComboBox combo = (JComboBox)editor.getComponent(); The JComboBox is wrapped in a CellEditor. Y ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。