创建新的xml文件
步骤:
1.得到DOM解析器的工厂实例DocumentBuilderFactory
2.从DOM工厂中获取DOM解析器
3创建得到DOM树
4创建节点,添加节点
5将刚创建的节点添加到另一个节点上
6将节点添加到document中
7保存xml文件
代码:
// 1.得到DOM解析器的工厂实例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 2.从DOM工厂里获取DOM解析器 DocumentBuilder db; try { db = dbf.newDocumentBuilder(); Document docs = db.newDocument(); //创建节点 Element noteElement = docs.createElement("note"); Element toElement = docs.createElement("to"); Text l = docs.createTextNode("333"); toElement.appendChild(l);//lableElement.setNodeValue(lable); Element fromElement = docs.createElement("from"); Text t = docs.createTextNode("321"); fromElement.appendChild(t);//titleElement.setNodeValue(title); Element headingElement = docs.createElement("heading"); Text x = docs.createTextNode("sadda"); headingElement.appendChild(x);//textElement.setNodeValue(text); String time=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()); Element bodyElement = docs.createElement("body"); Text m = docs.createTextNode(time); bodyElement.appendChild(m); //添加父子关系 noteElement.appendChild(toElement); noteElement.appendChild(fromElement); noteElement.appendChild(headingElement); noteElement.appendChild(bodyElement); //Element docsElement=(Element)docs.getElementsByTagName("docs").item(0); //docsElement.appendChild(docElement); //将节点添加到document中 docs.getDocumentElement().appendChild(noteElement); //保存xml文件 TransformerFactory transformerFactory=TransformerFactory.newInstance(); Transformer transformer=transformerFactory.newTransformer(); DOMSource domSource=new DOMSource(docs); //设置编码类型 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); File f=new File("e:\\Test.xml"); StreamResult result=new StreamResult(new FileOutputStream(f)); //把DOM树转换为xml文件 transformer.transform(domSource, result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
追加内容:
package com.test; import java.io.File; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.Date; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; public class TestXml { public static void main(String[] args) { // 1.得到DOM解析器的工厂实例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 2.从DOM工厂里获取DOM解析器 DocumentBuilder db; try { db = dbf.newDocumentBuilder(); // 3.解析XML文档,得到DOM树 File f=new File("e:\\Test.xml"); Document docs = db.parse(f); //创建节点 Element noteElement = docs.createElement("note"); Element toElement = docs.createElement("to"); Text l = docs.createTextNode("333"); toElement.appendChild(l);//lableElement.setNodeValue(lable); Element fromElement = docs.createElement("from"); Text t = docs.createTextNode("321"); fromElement.appendChild(t);//titleElement.setNodeValue(title); Element headingElement = docs.createElement("heading"); Text x = docs.createTextNode("sadda"); headingElement.appendChild(x);//textElement.setNodeValue(text); String time=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()); Element bodyElement = docs.createElement("body"); Text m = docs.createTextNode(time); bodyElement.appendChild(m); //添加父子关系 noteElement.appendChild(toElement); noteElement.appendChild(fromElement); noteElement.appendChild(headingElement); noteElement.appendChild(bodyElement); docs.getDocumentElement().appendChild(noteElement); //保存xml文件 TransformerFactory transformerFactory=TransformerFactory.newInstance(); Transformer transformer=transformerFactory.newTransformer(); DOMSource domSource=new DOMSource(docs); //设置编码类型 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result=new StreamResult(new FileOutputStream(f)); //把DOM树转换为xml文件 transformer.transform(domSource, result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }