自己写的dom添加节点程序,但出错,大家帮着看看
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class Insert{
static Document doc1;
public static void main(String args[]){
try{
DocumentBuilderFactory db1 =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = db1.newDocumentBuilder();
doc1= db.parse("jobrename.xml");
}
catch (Exception e)
{
System.err.println("Sorry, an error occurred: " + e);
}
Element root =doc1.getDocumentElement();
System.out.println(root);
Element jobtype= root.createElement("zhaochong");
jobtype.setNodeValue("wawa");
root.appendChild(jobtype);
}
}
xml文档为
?xml version="1.0" encoding="UTF-8"?>
<jobrename>
<qiongju>
<location>haha</location>
</qiongju>
<zheban>
<location>haha</location>
</zheban>
</jobrename>
运行时报错,想了好久没有结果,苦闷啊