java-Dom创建和解析xml

===== ======================
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();

}

}

}
 
=========================== =========================== =========================== ======================
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值