0

我有一个现有的空白 XML 文件,如下所示:

<root>
<!--Some Comment goes here>
</root>

现在我想要一个 GUI,它有 1 个文本字段和 1 个文本区域,它需要一些字符串(标题和内容),然后单击“保存”按钮,它将它们添加到元素下的 XML 文件中。我在单独的 GUI 中创建了根目录并保存到“sample.xml”中,单击按钮后,会出现内容添加窗口,我在其中打开相同的 XML 文件并希望将项目添加到其中。但每次我这样做时,我都会收到“空指针异常”错误。以下是我的代码。请告诉我哪里出错了:

主 GUI 中的代码:

JButton btnNewButton = new JButton("Add New");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //File f = new File("sample.xml");
                DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder;
                try {
                    Writer output = new BufferedWriter(new FileWriter("sample.xml"));
                    docBuilder = dbfac.newDocumentBuilder();
                    Document doc = docBuilder.newDocument();

                    Element root = doc.createElement("root");
                    doc.appendChild(root);

                    Comment comment = doc.createComment("Just a thought");
                    root.appendChild(comment);

                    TransformerFactory transfac = TransformerFactory.newInstance();
                    Transformer trans = transfac.newTransformer();
                    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                    trans.setOutputProperty(OutputKeys.INDENT, "yes");

                    StringWriter sw = new StringWriter();
                    StreamResult result = new StreamResult(sw);
                    DOMSource source = new DOMSource(doc);
                    trans.transform(source, result);
                    String xmlString = sw.toString();

                    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("sample.xml", true)));
                    out.println(xmlString);
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (TransformerException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

在辅助 GUI 中添加元素的代码:

JButton btnSave = new JButton("Save");
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                dbfac.setIgnoringComments(true);
                DocumentBuilder docBuilder;
                try {
                    docBuilder = dbfac.newDocumentBuilder();
                    Document doc = docBuilder.parse("sample.xml");

                    NodeList rt = doc.getElementsByTagName("root");

                    Element child = doc.createElement("content");
                    child.setAttribute("title", textField.getText().toString());
                    rt.item(0).appendChild(child);

                    Text text = doc.createTextNode(textArea.getText().toString());
                    child.appendChild(text);

                    TransformerFactory transfac = TransformerFactory.newInstance();
                    Transformer trans = transfac.newTransformer();
                    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                    trans.setOutputProperty(OutputKeys.INDENT, "yes");

                    StringWriter sw = new StringWriter();
                    StreamResult result = new StreamResult(sw);
                    DOMSource source = new DOMSource(doc);
                    trans.transform(source, result);
                    String xmlString = sw.toString();

                    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("sample.xml", true)));
                    out.println(xmlString);
                    out.close();


                    System.out.println(xmlString);
                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                } catch (TransformerConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (TransformerException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



            }
        });
4

0 回答 0