IT程式員開發必備-各類資源下載清單,史上最全IT資源,個人收藏總結。 package edu.jaxp;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* 測試對document對象進行CRUD操作
**/
public class JaxpDomCRUD {
//解析xml文檔,得到代表文檔的document
public Document getDocument(){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document document = null;
try {
builder = factory.newDocumentBuilder();
document = builder.parse("WebRoot/product1.xml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return document;
}
//將記憶體中修改的document對象更新至xml文檔中
public void updateXML(Document documentSrc) {
//得到TransformerFactory執行個體對象
TransformerFactory transformerFactory = TransformerFactory.newInstance();
//由transformerFactory對象建立Transformer對象
Transformer transformer = null;
try {
transformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/*
* 調用transformer對象的transform(Source xmlSource, Result outputTarget)方法
* public class DOMSource extends Object implements Source
* 構造方法:DOMSource(Node n)
* public class StreamResult extends Object implements Result
* 構造方法:StreamResult(OutputStream outputStream)
*/
try {
transformer.transform(new DOMSource(documentSrc),
new StreamResult(new FileOutputStream("WebRoot/product1.xml")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//向<product category="Table">節點中添加節點<notes>這是桌椅</notes>
@Test
public void appendNode(){
//得到xml文檔的document對象
Document document = getDocument();
//建立<notes>新節點
Element notes = document.createElement("notes");
notes.setAttribute("name","肖華");
notes.setTextContent("這是桌椅");
//把建立的新節點追加在第二個<product>節點下
Element product = (Element) document.getElementsByTagName("product").item(1);
product.appendChild(notes);
//更新XML文檔
updateXML(document);
}
//向xml文檔中指定位置上添加節點:<notes>這是桌椅</notes>
@Test
public void insertNode(){
//得到xml文檔的document對象
Document document = getDocument();
//建立<notes>新節點
Element notes = document.createElement("notes");
notes.setAttribute("name","肖華");
notes.setTextContent("這是桌椅");
//得到參考節點
Element refNode = (Element) document.getElementsByTagName("price").item(1);
//把建立的新節點插入在第二個<product>節點下的子節點<price>前面
Element product = (Element) document.getElementsByTagName("product").item(1);
product.insertBefore(notes, refNode);
//更新XML文檔
updateXML(document);
}
//向xml文檔指定節點添加或修改或刪除屬性
@Test
public void setOrUpdateAttr(){
//得到xml文檔的document對象
Document document = getDocument();
//為 <notes>這是扳手</notes>節點添加屬性
Element notesNode = (Element) document.getElementsByTagName("notes").item(0);
notesNode.setAttribute("name", "曹歡");
//為<price street="澳門街" wholesale="部分">100.0</price>修改屬性值
Element priceNode = (Element) document.getElementsByTagName("price").item(1);
priceNode.setAttribute("wholesale", "小部分");
// 移除<specifications weight="2.0kg">扳手</specifications>的weight屬性
Element specificationsNode = (Element) document.getElementsByTagName("specifications").item(0);
specificationsNode.removeAttribute("weight");
//更新XML文檔
updateXML(document);
}
//刪除xml文檔中指定的節點<notes name="曹歡">這是扳手</notes>
@Test
public void deleteNode(){
//得到xml文檔的document對象
Document document = getDocument();
//取得要刪除的節點
Element notesNode = (Element) document.getElementsByTagName("notes").item(0);
//取得要刪除的節點的父節點,如果noteNode節點為null的話,則會拋出NullPointerException
//Element parentNode = (Element) notesNode.getParentNode();
Element parentNode = (Element) document.getElementsByTagName("product").item(1);
//由父節點移除子節點
parentNode.removeChild(notesNode);
//更新XML文檔
updateXML(document);
}
//修改Element元素的內容
@Test
public void updateContextText(){
//得到xml文檔的document對象
Document document = getDocument();
//priceNode只能是productNode節點的子節點
Element productNode = (Element) document.getElementsByTagName("product").item(0);
Element priceNode = (Element) productNode.getElementsByTagName("price").item(0);
priceNode.setTextContent("90.0");
//更新XML文檔
updateXML(document);
}
}
product1.xml檔案內容:
|
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<catalog id="cata1">
<product category="HandTool">
<specifications>扳手</specifications>
<price street="香港街">90.0</price>
</product>
<product category="Table">
<specifications>桌椅</specifications>
<notes name="肖華">這是桌椅</notes>
<price street="澳門街" wholesale="小部分">100.0</price>
</product>
</catalog> |