1.DOM的常用方法
1)Document接口常用方法
方法 | 說明 |
---|---|
public NodeList getELementsByTagName(String tagName) | 取得指定名稱的NodeList |
public Element createElement(String tagName) throws DOMException | 創建一個指定名稱的節點 |
public Text createTextNode(String data) | 創建一個文本內容節點 |
Element createElement(String tagName) throws DOMException | 創建一個節點元素 |
public Attr createAttribute(String name) throws DOMException | 創建一個屬性 |
2)Node接口常用方法
方法 | 說明 |
---|---|
Node appendChild(Node newChile) throws DOMException | 在當前節點下增加一個新節點 |
public NodeList getChildNodes() | 取得本節點的全部子節點 |
public Node getFirstChild() | 取得本節點的第一個子節點 |
public Node getLastChild() | 取得本節點的最後一個子節點 |
public boolean hasChildNodes() | 判斷是否還有其他節點 |
public boolean hasAttributes() | 判斷是否還有其他屬性 |
String getNodeValue() throws DOMException | 取得節點內容 |
3)NodeList接口常用方法
方法 | 說明 |
---|---|
public int getLength() | 取得節點個數 |
public Node item(int index) | 根據索引取得節點對象 |
4)StreamResult類的構造方法
方法 | 說明 |
---|---|
public StreamResult(File f) | 指定輸出文件 |
public StreamResult(OutputStream outputStream) | 指定輸出的輸出流 |
2.DOM解析XML文件
//DOM解析XML文件示例
//假設已導入所需的包
public class Main {
public static void main(String args[]) {
//建立DocumentBuilderFactory,用於取得DocumentBuilder
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder bui = null;
try {
//通過DocumentBuilderFactory,取得DocumentBuilder
bui = fac.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = null;
try {
//讀取指定路徑的xml文件
doc = bui.parse("C:" +
File.separator + "DomDemo1.xml");
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//查找'link'節點
NodeList nl = doc.getElementsByTagName("link");
//輸出nl中,第一個子節點中的文本節點內容
for (int x = 0; x < nl.getLength(); x++) {
Element e = (Element) nl.item(x);
/*
item(0).getFirstChile()爲取得第一個子節點的第一個文本節點
但是若是'link'中有多個節點,就可以改變index,
如:改爲item(1)就會輸出第二個name的value
*/
System.out.print("name:" + e.getElementsByTagName("name").item
(0).getFirstChild().getNodeValue());
System.out.println(" age:" + e.getElementsByTagName("age").item
(0).getFirstChild().getNodeValue());
}
}
}
3.DOM生成XML文件
//DOM生成XML文件示例
//假設已導入所需的包
public class Main {
public static void main(String args[]) {
//建立DocumentBuilderFactory,以取得DocumentBuilder
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder bul = null;
try {
//通過DocumentBuilderFactory,取得DocumentBuilder
bul = fac.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
//定義Document接口對象,通過DocumentBuilder類進行DOM樹的轉換操作
Document doc = null;
//創建一個新文檔
doc = bul.newDocument();
//建立各個操作節點
Element stulist = doc.createElement("stulist");
Element link1 = doc.createElement("link");
Element link2 = doc.createElement("link");
Element name1 = doc.createElement("name");
Element age1 = doc.createElement("age");
Element name2 = doc.createElement("name");
Element age2 = doc.createElement("age");
//設置文本內容
name1.appendChild(doc.createTextNode("aa"));
age1.appendChild(doc.createTextNode("17"));
name2.appendChild(doc.createTextNode("bb"));
age2.appendChild(doc.createTextNode("18"));
//設置節點關係
link1.appendChild(name1); //子節點
link1.appendChild(age1);
link2.appendChild(name2);
link2.appendChild(age2);
stulist.appendChild(link1);
stulist.appendChild(link2);
doc.appendChild(stulist); //文檔上保存節點
//輸出文檔到文件中
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = null;
try {
t = tf.newTransformer();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
//設置字符編碼
t.setOutputProperty(OutputKeys.ENCODING,"utf-8");
//輸出文檔
DOMSource sou = new DOMSource(doc);
//指定輸出位置
StreamResult res = new StreamResult(new File("C:" + File.separator + "DomDemo2.xml"));
try {
//輸出
t.transform(sou,res);
} catch (TransformerException e) {
e.printStackTrace();
}
}
}