package com.hunau.example.dom;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
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.NodeList;
import org.xml.sax.SAXException;
/*
* Author:pidty
* Discription:using dom to builid a xml file
* Time:2011-4-18
*/
public class DomDemo {
//创建xml文档
public static void createXmlFile(DocumentBuilder dBuilder){
//得到文档对象
Document doc = dBuilder.newDocument();
//创建文档根节点
Element root = doc.createElement("User");
//创建一级子节点
Element user1 = doc.createElement("user");
user1.setAttribute("id", "001");
Element user2 = doc.createElement("user");
user2.setAttribute("id", "002");
//创建二级子节点
Element name1 = doc.createElement("姓名");
Element age1 = doc.createElement("年龄");
Element sex1 = doc.createElement("性别");
Element name2 = doc.createElement("姓名");
Element age2 = doc.createElement("年龄");
Element sex2 = doc.createElement("性别");
//将二级节点设置值
name1.appendChild(doc.createTextNode("pidty"));
age1.appendChild(doc.createTextNode("20"));
sex1.appendChild(doc.createTextNode("男"));
name2.appendChild(doc.createTextNode("keary"));
age2.appendChild(doc.createTextNode("22"));
sex2.appendChild(doc.createTextNode("女"));
//将二级子节点添加到一级子节点中
user1.appendChild(name1);
user1.appendChild(age1);
user1.appendChild(sex1);
user2.appendChild(name2);
user2.appendChild(age2);
user2.appendChild(sex2);
//将一级子节点添加到根节点
root.appendChild(user1);
root.appendChild(user2);
//将根节点添加到文档中
doc.appendChild(root);
//调用写入方法,将doc写到磁盘
writeXmlFile(doc,new File("1.xml"));
}
//为id为001的节点添加pid属性
public static void addAttribute(DocumentBuilder dBuilder){
Document doc = null;
try {
doc = dBuilder.parse(new File("1.xml"));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//得到文档的根元素
Element root = doc.getDocumentElement();
//得到一级子节点的所有节点
NodeList userList = doc.getElementsByTagName("user");
Element userElement = null;
for(int i=0;i<userList.getLength();i++){
userElement = (Element)userList.item(i);
if(userElement.getAttribute("id").equals("001")){
userElement.setAttribute("pid", "123");
}
}
//重新将修改后的节点添加到根节点中
root.appendChild(userElement);
//调用写入方法
writeXmlFile(doc,new File("2.xml"));
}
//将创建的XML文件写入磁盘
public static void writeXmlFile(Document doc,File file){
//获得工厂方法
TransformerFactory tFactory = TransformerFactory.newInstance();
try {
//获得Transformer实例
Transformer transformer = tFactory.newTransformer();
//调用写入方法
transformer.transform(new DOMSource(doc), new StreamResult(file));
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
//修改id为001的节点的id属性
public static void modifyAttribute(DocumentBuilder dBuilder){
Document doc = null;
try {
doc = dBuilder.parse("1.xml");
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element root = doc.getDocumentElement();
NodeList userList = doc.getElementsByTagName("user");
Element userElement = null;
for(int i=0;i<userList.getLength();i++){
userElement = (Element)userList.item(i);
if(userElement.getAttribute("id").equals("001")){
//修改id值为001的属性
userElement.setAttribute("id", "101");
}
}
root.appendChild(userElement);
//调用writeXmlFile()
writeXmlFile(doc, new File("3.xml"));
}
//删除id为001的节点的id属性
public static void removeAttribute(DocumentBuilder dBuilder){
Document doc = null;
try {
doc = dBuilder.parse("1.xml");
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element root = doc.getDocumentElement();
NodeList userList = doc.getElementsByTagName("user");
Element userElement = null;
for(int i=0;i<userList.getLength();i++){
userElement = (Element)userList.item(i);
if(userElement.getAttribute("id").equals("001")){
//删除id值为001的属性
userElement.removeAttribute("id");
}
}
root.appendChild(userElement);
//调用writeXmlFile()
writeXmlFile(doc, new File("4.xml"));
}
//修改id为001的age值
public static void modifyNodeValue(DocumentBuilder dBuilder){
Document doc = null;
try {
doc = dBuilder.parse("1.xml");
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element root = doc.getDocumentElement();
NodeList userList = doc.getElementsByTagName("user");
Element userElement = null;
for(int i=0;i<userList.getLength();i++){
userElement = (Element)userList.item(i);
if(userElement.getAttribute("id").equals("001")){
//获得叶子节点值
String nodeValue = userElement.getElementsByTagName("年龄").item(0).getFirstChild().getNodeValue();
System.out.println(nodeValue);
userElement.getElementsByTagName("年龄").item(0).getFirstChild().setNodeValue("30");
}
}
//调用writeXmlFile()
writeXmlFile(doc, new File("5.xml"));
}
//增加id为001的节点的子节点(电话)
public static void addNode(DocumentBuilder dBuilder){
Document doc = null;
try {
doc = dBuilder.parse("1.xml");
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element root = doc.getDocumentElement();
Element tel = doc.createElement("电话");
tel.appendChild(doc.createTextNode("13856234569"));
NodeList userList = doc.getElementsByTagName("user");
Element userElement = null;
for(int i=0;i<userList.getLength();i++){
userElement = (Element)userList.item(i);
if(userElement.getAttribute("id").equals("001")){
userElement.appendChild(tel);
}
}
root.appendChild(userElement);
//调用writeXmlFile()
writeXmlFile(doc, new File("6.xml"));
}
public static void main(String[] args) {
//生成DocumentBuilderFactory实例
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
//生成DocumentBuilder实例
DocumentBuilder dBuilder = null;
try {
dBuilder = dbFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
//1.调用createXmlFile()方法
// createXmlFile(dBuilder);
//2.为一级子节点添加属性
// addAttribute(dBuilder);
//3.修改一级子节点属性值
// modifyAttribute(dBuilder);
//4.删除一级子节点属性值
// removeAttribute(dBuilder);
//5.修改id值为001的age的值
// modifyNodeValue(dBuilder);
//6.为id值为001的user节点添加子节点tel
addNode(dBuilder);
System.out.println("完成操作");
}
}
**************************************************************
上面的各个方法是单独写出来的,有不少冗余,主要是为了更加清晰的显示出操作步骤,有兴趣的可以
自己把上面的几个方法中的公共部分提出来形成一个单独的方法。。。