DOM的编程API:
Node:节点
Document :根节点 表示整个文档
NodeList:节点的集合
NamedNodeMap:一般用于存储属性
Element:
Text
Attr
编程思路:
1.获得DocumentBuilderFactory的实例
2.通过工厂获得DocumentBuilder的解析器
3.parse(File)======>Document
-----------------------------------------------------------------------------------------------------------
importjavax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
//通过DOM方式解析XML文档
public class MyDom{
publicstatic void main(String args[])throws Exception{
//1.得到工厂类(DocumentBuilderFactory)的实例
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
//2.得到解析器实例
DocumentBuilder builder=factory.newDocumentBuilder();
//3.使用parse()解析文件 返回 文档的根节点Document
Documentdoc=builder.parse(new File("student.xml"));
Element root=doc.getDocumentElement();
System.out.println("根元素是:"+root.getTagName());
NodeListnl=doc.getElementsByTagName("student");
//NodeListnl=root.getChildNodes();
//遍历NodeList
for(inti=0;i<nl.getLength();i++){
Node node=nl.item(i); //得到Node集合中的每一个
NamedNodeMap nnm=node.getAttributes();
for(int j=0;j<nnm.getLength();j++){
Nodeatt_node=nnm.item(j);
Attrattr=(Attr)att_node;
Stringatt_name=attr.getName();
Stringattvalue=attr.getValue();
System.out.println(att_name+"="+att_value);
}
Element first=(Element)node;
System.out.println(first.getTagName());
//得到每个student元素的子节点
NodeList secs=node.getChildNodes();
System.out.println(secs.getLength());
for(intk=0;k<secs.getLength();k++){
//得到二级子节点
Node sec_node=secs.item(k);
//过滤空白
if(sec_node.getNodeType()==Node.ELEMENT_NODE){
Elementsec=(Element)sec_node;
System.out.println(sec.getTagName());
System.out.println(sec.getTextContent());
}
}
}
}
}
-----------------------------------------------------------
---------------Create.java---------
import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
importjavax.xml.transform.dom.DOMSource;
importjavax.xml.transform.stream.StreamResult;
public class Create{
publicstatic void main(String args[])throws Exception{
//1.获得工厂实例
DocumentBuilderFactoryf=DocumentBuilderFactory.newInstance();
//2.得到解析器
DocumentBuilder builder=f.newDocumentBuilder();
//3.builder.newDocument();
Document doc=builder.newDocument();
//创建元素
Element root=doc.createElement("emps");
Elementemp1=doc.createElement("emp");
Elementname1=doc.createElement("name");
name1.setTextContent("etoak");
Elementemail1=doc.createElement("email");
email1.setTextContent("email1");
Elementemp2=doc.createElement("emp");
Elementname2=doc.createElement("name");
name2.setTextContent("etoak1");
Elementemail2=doc.createElement("email");
email2.setTextContent("email1");
//组装 appendChild() Node
doc.appendChild(root);
root.appendChild(emp1);
root.appendChild(emp2);
emp1.appendChild(name1);
emp1.appendChild(email1);
emp2.appendChild(name2);
emp2.appendChild(email2);
//将DOcument实例 持久化到磁盘上
//1.TransformerFactory 实例 newInstance();
TransformerFactory factory=TransformerFactory.newInstance();
//2.factory实例得到 Transformer 实例
Transformer former=factory.newTransformer();
former.setOutputProperty(OutputKeys.INDENT,"yes");
//构造源
DOMSource source=new DOMSource(doc);
//构造结果 类型stream
StreamResult result=new StreamResult(newFile("emp.xml"));
former.transform(source,result);
}
}