=====
======================
=========================== =========================== =========================== ======================
package com.cnse.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* @author Administrator
* info dom创建和解析xml
*/
public class DomXml {
public static void main(String[] args) throws FileNotFoundException,
ParserConfigurationException {
// 创建xml
createXml();
// 解析xml
//parseXml();
}
/**
* dom创建xml
* @throws ParserConfigurationException
* @throws FileNotFoundException
*/
private static void createXml() throws ParserConfigurationException,FileNotFoundException {
// 创建dom生成工厂
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
//定义文档
Document doc = db.newDocument();
Element students = doc.createElement("students");
doc.appendChild(students);
Element student = doc.createElement("student");
student.setAttribute("id", "001");
Element name = doc.createElement("name");
name.appendChild(doc.createTextNode("张三"));
student.appendChild(name);
Element sex = doc.createElement("sex");
sex.appendChild(doc.createTextNode("男"));
student.appendChild(sex);
Element age = doc.createElement("age");
age.appendChild(doc.createTextNode("26"));
student.appendChild(age);
students.appendChild(student);
try {
TransformerFactory tsf = TransformerFactory.newInstance();
Transformer tf = tsf.newTransformer();
DOMSource domSource = new DOMSource(doc);
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
File f = new File("E:\\students.xml");
FileOutputStream out = new FileOutputStream(f);
StreamResult xmlResult = new StreamResult(out);
System.out.println("生成到磁盘成功.....");
tf.transform(domSource, xmlResult);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* dom 解析xml的方法
*/
public static void parseXml() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File fileName = new File("E:\\students.xml");
Document document = builder.parse(fileName);
Element root = document.getDocumentElement();
// 便利所有的子节点
NodeList childList = root.getChildNodes();
System.out.println("根节点名称::" + root.getNodeName());
for (int i = 0; i < childList.getLength(); i++) {
// 获取根节点下的所有子节点
Node child = childList.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE)
System.out.println("根节点下的所有子节点:::::::" + child);
// 输出child的属性;
if (child.getNodeType() == Node.ELEMENT_NODE) {
// System.out.println(child.getAttributes().getNamedItem("email").getNodeValue());
System.out.println("字节点的属性值:::::"
+ child.getAttributes().getNamedItem("id"));
}
// 便利所有的孙子节点
NodeList szlist = child.getChildNodes();
for (int z = 0; z < szlist.getLength(); z++) {
Node sz = szlist.item(z);
if (sz.getNodeType() == Node.ELEMENT_NODE)
System.out.println("孙子节点的__名:::::::::"
+ sz.getNodeName());
if (sz.getNodeType() == Node.ELEMENT_NODE)
System.out.println("孙子节点的__值;;;;;;;"
+ sz.getFirstChild());
}
System.out.println("\n");
// ======================
for (Node sz = child.getFirstChild(); sz != null; sz = sz
.getNextSibling()) {
if (sz.getNodeType() == Node.ELEMENT_NODE) {
if ("name".equals(sz.getNodeName())) {
System.out.println(sz.getFirstChild()
.getNodeValue());
}
}
if (sz.getNodeType() == Node.ELEMENT_NODE) {
if ("sex".equals(sz.getNodeName())) {
System.out.println(sz.getFirstChild()
.getNodeValue());
}
}
if (sz.getNodeType() == Node.ELEMENT_NODE) {
if ("age".equals(sz.getNodeName())) {
System.out.println(sz.getFirstChild()
.getNodeValue());
}
}
}
// ======================
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
=========================== =========================== =========================== ======================