我有一个像这样的空白XML文件:
<root>
<!--Some Comment goes here>
</root>
现在我想拥有一个GUI,其中包含1个textfield和1个textarea,它需要一些字符串(标题和内容),点击“保存”按钮后,它会将它们添加到元素下的XML文件中。我在一个单独的GUI中创建了root并保存到'sample.xml'中,单击一个按钮后,会出现内容添加窗口,我打开相同的XML文件,并希望将项目添加到该文件中。但每次我这样做,我都会收到'Null Pointer Exception'错误。以下是我的代码。请告诉我哪里出错了:
主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();
}
}
});