将节点添加到根节点时发生java.lang.NullPointerException
我正在尝试通过以下函数将标记及其值动态添加到xml文件中。我正在尝试将名为first name的标记及其值添加到根标记下。但在运行以下代码段时,我遇到了异常将节点添加到根节点时发生java.lang.NullPointerException,java,xml,xml-parsing,nullpointerexception,Java,Xml,Xml Parsing,Nullpointerexception,我正在尝试通过以下函数将标记及其值动态添加到xml文件中。我正在尝试将名为first name的标记及其值添加到根标记下。但在运行以下代码段时,我遇到了异常 public void write(String name) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuild
public void write(String name) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
Element blobKey_E = document.createElement("first-name");
blobKey_E.appendChild( document.createTextNode( name ) );
// The following line produces an exception
// LINE 27
document.getDocumentElement().appendChild(blobKey_E); // append the new tag under the root
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File("/home/non-admin/NetBeansProjects/Personal Site_Testers/web/xml/xml_1.xml"));
transformer.transform(source, result);
}catch(Exception exc) {
exc.printStackTrace();
}
}
例外情况:
我在上面的函数中突出显示了第27行。
document.getDocumentElement()
在本例中返回null
forgetDocumentElement()
说明了以下功能:
This is a convenience attribute that allows direct access to the child node that is the root element of the document.
在您的情况下,没有根
元素
附加到您的DOM。您可能需要执行文档.appendChild(blobKey_E)
将blobKey\u E
作为根元素附加到DOM
理想情况下,当您尝试构建XML DOM时,以下是您需要遵循的基本步骤:
创建文档创建根元素并将其添加到文档中
创建子元素并将其附加到根节点或其他现有子节点
public void write(String name) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
Element blobKey_E = document.createElement("first-name");
blobKey_E.appendChild( document.createTextNode( name ) );
/*
* Here blobKey_E is treated as the root element for the document that you've created
*/
document.appendChild(blobKey_E);
// // LINE 27
// document.getDocumentElement().appendChild(blobKey_E); // append the new tag under the root
/*
* Post this point, if you do a document.getDocumentElement(), it will no longer return
* a nullpointerexception because blobKey_E will be treated as the root element.
*/
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File("/home/non-admin/NetBeansProjects/Personal Site_Testers/web/xml/xml_1.xml"));
transformer.transform(source, result);
}catch(Exception exc) {
exc.printStackTrace();
}
}
document元素是根元素,您需要设置它。将第27行改为
document.appendChild(blobKey_E);
那么,代码的哪一行是第27行?您是否已将其放入调试器并四处查看过?空指针异常似乎表明
document
或document.getDocumentElement()
为空。@b argulies编辑了问题,因为您从未创建过document元素,这一点也不奇怪。文档元素不是根元素;它是命名的顶部元素,您需要创建它并将其添加到根中。@Y.E.P:errm,您的代码永远不会显示您创建根节点并附加到已创建的文档的位置。那部分在哪里?我希望所有这些都在xml中已经存在的根元素下document@Y.E.P:除非你告诉我哪一个是你的根元素
元素
,否则我帮不了你多少忙!xml文件中已经写入了根元素。我想把标签的名字放在下面
document.appendChild(blobKey_E);