publicstatic void main(String[] args) ...{
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
Element theBook=null, theElem=null,root=null;
try ...{
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db=factory.newDocumentBuilder();
Documentxmldoc=db.parse(new File("Test1.xml"));
root=xmldoc.getDocumentElement();
//--- 新建一本书开始 ----
theBook=xmldoc.createElement("book");
theElem=xmldoc.createElement("name");
theElem.setTextContent("新书");
theBook.appendChild(theElem);
theElem=xmldoc.createElement("price");
theElem.setTextContent("20");
theBook.appendChild(theElem);
theElem=xmldoc.createElement("memo");
theElem.setTextContent("新书的更好看。");
theBook.appendChild(theElem);
root.appendChild(theBook);
System.out.println("--- 新建一本书开始 ----");
output(xmldoc);
//--- 新建一本书完成 ----