DOM(Document Object Model)
1.文档节点(org.w3c.dom.Document接口)
public interface Document extends Node {
public Element getDocumentElement();
//获得文档的根节点
public Attr createAttribute(String name)throws DOMException;
//创建属性节点
public Attr createAttributeNS(String namespaceURI,String qualifiedName)
throws DOMException;//根据名称空间URL和限定名创建属性节点
public CDATASection createCDATASection(String data)throws DOMException;//创建CDATA段
public Comment createComment(String data);//创建注释
public Element createElement(String tagName)throws DOMException;//创建元素
public Element getElementById(String elementId); //根据ID属性的值,获得Element
public NodeList getElementsByTagName(String tagname);//根据TagName获得Element的列表NodeList
public NodeList getElementsByTagNameNS(String namespaceURI,String localName);//名称空间(同上)
}
2.元素节点(org.w3c.dom.Element接口)
public interface Element extends Node{
public String getAttribute(String name);//通过属性名获得属性值
public Attr getAttributeNode(String name);//获得属性节点
public Attr getAttributeNodeNS(String namespaceURI,String localName)
throws DOMException;//获得属性节点(名称空间)
public boolean hasAttribute(String name);//是否用属性
public void removeAttribute(String name)throws DOMException;//如果删除的属性有缺省值,就出会出现缺省值
public void setAttribute(String name,String value)throws DOMException;//为一个元素设置属性
public Attr setAttributeNode(Attr newAttr);//增加一个新的属性节点
}
3.文本节点(org.w3c.dom.Text接口)
文本节点包括空白
4.属性节点(org.w3c.dom.Attr接口)
public interface Attr extends Node {
public String getName();
public String getValue();
public void setValue(String value)
public boolean isId();
}
5.注释节点(org.w3c.dom.Comment接口)
public interface Comment extends CharacterData {}
6.处理指令节点(org.w3c.dom.ProcessingInstruction接口)
public interface ProcessingInstruction extends Node {}
7.文档类型节点(org.w3c.dom.DocumentType接口)
每个Document都有DocumentType属性
public interface DocumentType extends Node {}
8.CDATA段节点(org.w3c.dom.CDATASection接口)
public interface CDATASection extends Text {}
9.文档片段节点(org.w3c.dom.DocumentFragment接口)
public interface DocumentFragment extends Node {}
10.实体节点
...
11.NodeList接口
public interface NodeList {
public Node item(int index);
public int getLength();
}
12.NamedNodeMap接口(用于属性)
public interface NamedNodeMap {
public int getLength();
public Node getNamedItem(String name);//根据名字返回节点
public Node item(int index);//根据index返回节点
}
//NodeList和NameNodeMap都是保存的对象的引用,所以对里面的对象进行操作时,会改变XML的结构等...
public class DomTest {
public static void main(String[] args){
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
System.out.println(dbf.getClass().getName());//查看解释器类型
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("students.xml"));
NodeList nl = doc.getElementsByTagName("student");//返回一个列表,里面装着这个标签的Node列表
int length = nl.getLength();//返回有多少个签标
for(int i=0;i<length;i++){
Element e = (Element) nl.item(i);//item(int i)表示取这个第几个元素
Node name = e.getElementsByTagName("name").item(0);
Node age = e.getElementsByTagName("age").item(0);
Text nameText = (Text) name.getFirstChild();
Text ageText = (Text) age.getFirstChild();
System.out.println("name = "+nameText.getTextContent());//getTextContent()获得文本的内容
System.out.println("age = "+ageText.getTextContent());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class DomPrint {
public static void printNodeInfo(Node node){
System.out.println("nodeName:"+node.getNodeName()+" nodeValue:"+node.getNodeValue());
//获得Node的姓字和值
}
public static void printNode(Node node){
short nodetype = node.getNodeType();//获得Node的获型
switch(nodetype){
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println("-----------------------PI Start-----------------------");
printNodeInfo(node);
System.out.println("-----------------------PI End-----------------------");
break;
case Node.ELEMENT_NODE:
System.out.println("-----------------------Element Start-----------------------");
printNodeInfo(node);
System.out.println("-----------------------Element End-----------------------");
NamedNodeMap att = node.getAttributes();
int length = att.getLength();
for(int i=0;i<length;i++){
Attr attribute = (Attr) att.item(i);
System.out.println("-----------------------Attr Start-----------------------");
printNodeInfo(attribute);
System.out.println("-----------------------Attr Start-----------------------");
}
break;
case Node.TEXT_NODE:
System.out.println("-----------------------Text Start-----------------------");
printNodeInfo(node);
System.out.println("-----------------------Text Start-----------------------");
break;
default:
break;
}
Node child = node.getFirstChild();//获得第一个孩子
while(child!=null){
printNode(child);
child=child.getNextSibling();//获得对象的下一个对象,同级
}
}
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("applicationContext_actions.xml"));
DomPrint.printNode(doc);
}
}
public class DomCRUD {
public static void main(String[] args){
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("students.xml"));
Element student = doc.createElement("student");
Element name = doc.createElement("name");
Element age = doc.createElement("age");
//createElement("age");创建标签
Text nameText = doc.createTextNode("xiaowang");
Text ageText =doc.createTextNode("26");
//createTextNode("")创建文本
name.appendChild(nameText);
age.appendChild(ageText);
//appendChild()添加
student.appendChild(name);
student.appendChild(age);
student.setAttribute("sn","03");
//setAttribute设置属性
Element root = doc.getDocumentElement();
//getDocumentElement()获得根元素
root.appendChild(student);
NodeList nl = doc.getElementsByTagName("student");
Element delnode = (Element) nl.item(0);
root.removeChild(delnode);
//removeChild()删除孩子
Element editNode = (Element) nl.item(0);
editNode.setAttribute("sn","01");
Element editName = (Element) editNode.getElementsByTagName("name").item(0);
Element editAge = (Element) editNode.getElementsByTagName("age").item(0);
editAge.getFirstChild().setNodeValue("19");
for(int i=0;i<nl.getLength();i++ ){
Element e = (Element) nl.item(i);
System.out.println(e.getAttribute("sn"));
Node names = e.getElementsByTagName("name").item(0);
Node ages = e.getElementsByTagName("age").item(0);
System.out.println(names.getFirstChild().getTextContent());
System.out.println(ages.getFirstChild().getTextContent());
}
//将内存的XML对象树输出到一个文件
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.setOutputProperty("encoding", "utf-8");
DOMSource doms = new DOMSource(doc);
StreamResult sr = new StreamResult(new File("newstudent.xml"));
tf.transform(doms,sr);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
dom解析XML
最新推荐文章于 2019-09-09 00:31:36 发布