DOM程式設計

ahesihua發表於2011-10-17

最近一直在糾結關於xml方面的知識,包括xml語法,分析xml的結構和語法:包括DTD 和schema的語法定義等.

特別是到了xml解析這部分,我們在面試的時候經常會被問到這樣的問題.

下面的一個例子是關於DOM解析的:

 

package com.j2ee14.ch4;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class DOMStudy {
	Document doc;
	
	public static void main(String[] args) throws Exception {
		DOMStudy domStudy=new DOMStudy();
		domStudy.genDocInstance();
		domStudy.genDocument();
		domStudy.printDoc();
		domStudy.modifyNode();
		domStudy.printDoc();;
		domStudy.removeNode();
		domStudy.printDoc();
		domStudy.replaceNode();
		domStudy.printDoc();
		domStudy.removeAttribute();
		domStudy.printDoc();
	}
	
	
	private void genDocInstance(){
		System.out.println("build a document instance...");
		
		try{
			DocumentBuilderFactory domFactory= DocumentBuilderFactory.newInstance();
			domFactory.setValidating(false);
			domFactory.setNamespaceAware(true);
			
			DocumentBuilder domBuilder=domFactory.newDocumentBuilder();
			domBuilder.setErrorHandler(new MyErrorHandler());
			doc=domBuilder.newDocument();
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
	
	private void genDocument(){
		System.out.println("fill the content of the document.....");
		
		Element root=doc.createElement("student");
		root.setAttribute("id", "001");
		Element item=doc.createElement("name");
		item.appendChild(doc.createTextNode("jeff"));
		root.appendChild(item);
		
		item=doc.createElement("age");
		item.appendChild(doc.createTextNode("24"));
		root.appendChild(item);
		
		item=doc.createElement("height");
		item.appendChild(doc.createTextNode("1.70"));
		root.appendChild(item);
		
		item=doc.createElement("classId");
		item.appendChild(doc.createTextNode("2010212160006"));
		root.appendChild(item);
		
		
		item=doc.createElement("address");		
		Element state=doc.createElement("state");
		state.appendChild(doc.createTextNode("beijing"));		
		Element city=doc.createElement("city");
		city.appendChild(doc.createTextNode("beijing"));		
		Element street=doc.createElement("street");
		street.appendChild(doc.createTextNode("fuxing road"));		
		item.appendChild(state);
		item.appendChild(city);
		item.appendChild(street);
		root.appendChild(item);
		
	    doc.appendChild(root);		
	}
	
	private void printDoc()throws Exception{
		System.out.println("====================");
		System.out.println("print the document....");
		OutputFormat outFormat=new OutputFormat(doc);
		StringWriter stringout=new StringWriter();
		
		XMLSerializer serial=new XMLSerializer(stringout,outFormat);
		serial.asDOMSerializer();
		
		serial.serialize(doc.getDocumentElement());
		System.out.println("STRXML ="+stringout.toString());
		
		
	}
	private void modifyNode(){
		System.out.println("modify the node value....");
		Element root=doc.getDocumentElement();
		Element name=(Element) root.getFirstChild();
		name.getFirstChild().setNodeValue("hesihua");		
	}
	
	private void removeNode(){
		System.out.println("remove the node....");
		Element root=doc.getDocumentElement();
		root.removeChild(root.getFirstChild().getNextSibling());		
	}

	private void replaceNode(){
		System.out.println("replace a node.....");
		Element root=doc.getDocumentElement();
		Element item=doc.createElement("profession");
		item.appendChild(doc.createTextNode("software"));
		root.replaceChild(item, root.getFirstChild());		
	}
	
	private void removeAttribute(){
		System.out.println("remove a attribute");
		Element root=doc.getDocumentElement();
		root.removeAttribute("id");
		
	}
}

 

上面的程式碼在執行後得到的結果如下:

build a document instance...
fill the content of the document.....
====================
print the document....
STRXML =<?xml version="1.0" encoding="UTF-8"?>
<student id="001"><name>jeff</name><age>24</age><height>1.70</height><classId>2010212160006</classId><address><state>beijing</state><city>beijing</city><street>fuxing road</street></address></student>
modify the node value....
====================
print the document....
STRXML =<?xml version="1.0" encoding="UTF-8"?>
<student id="001"><name>hesihua</name><age>24</age><height>1.70</height><classId>2010212160006</classId><address><state>beijing</state><city>beijing</city><street>fuxing road</street></address></student>
remove the node....
====================
print the document....
STRXML =<?xml version="1.0" encoding="UTF-8"?>
<student id="001"><name>hesihua</name><height>1.70</height><classId>2010212160006</classId><address><state>beijing</state><city>beijing</city><street>fuxing road</street></address></student>
replace a node.....
====================
print the document....
STRXML =<?xml version="1.0" encoding="UTF-8"?>
<student id="001"><profession>software</profession><height>1.70</height><classId>2010212160006</classId><address><state>beijing</state><city>beijing</city><street>fuxing road</street></address></student>
remove a attribute
====================
print the document....
STRXML =<?xml version="1.0" encoding="UTF-8"?>
<student><profession>software</profession><height>1.70</height><classId>2010212160006</classId><address><state>beijing</state><city>beijing</city><street>fuxing road</street></address></student>

 

真覺得DOM解析很強大...

 

相關文章