標籤:style blog io color ar java for sp 檔案
獲得Document
/**
* 擷取文檔
* 1、獲得執行個體工廠
* 2、獲得解析器
* 3、獲得document
*/
添加結點
/**
* 1、獲得根結點
* 2、建立結點(設定屬性)
* 3、建立子節點()
* 4、添加到根節點
*/
更新結點
/**
* 更新XML
* 1、獲得所有結點
* 2、獲得每一本書
* 3、獲得ID
* 4、更新
* *1、更新id
* *2、更新子節點
*/
刪除結點
/**
* 刪除XML
* 1、獲得所有結點
* 2、獲得每一本書
* 3、獲得ID
* 4、刪除
*/
儲存XML
/**
* 儲存xml
* 1、獲得持久化對象執行個體工廠
* 2、獲得持久化對象
* 3、將記憶體資料儲存到磁碟
* *源————將document對象封裝到Source
* *結果————將檔案路徑封裝到Result
* 4、儲存
*/
1 public static void main(String[] args) throws Exception { 2 //擷取document 3 Document document=getDocument(); 4 //更新xml 5 //UpdateXml(document); 6 7 //添加XML 8 9 //AddXML(document); 10 11 12 //刪除xml 13 //DeleXML(document); 14 15 16 17 /** 18 * 儲存 19 */ 20 Save(document); 21 22 } 23 24 /** 25 * 1、獲得根結點 26 * 2、建立結點(設定屬性) 27 * 3、建立子節點() 28 * 4、添加到根節點 29 */ 30 private static void AddXML(Document document) { 31 Element rootElement=document.getDocumentElement(); 32 33 Element newElement=document.createElement("book"); 34 35 newElement.setAttribute("id", "b003"); 36 37 Element PriceElement=document.createElement("title"); 38 PriceElement.setTextContent("30"); 39 PriceElement.setAttribute("unit", "¥"); 40 newElement.appendChild(PriceElement); 41 42 rootElement.appendChild(newElement); 43 } 44 45 /** 46 * 刪除XML 47 * 1、獲得所有結點 48 * 2、獲得每一本書 49 * 3、獲得ID 50 * 4、刪除 51 */ 52 53 private static void DeleXML(Document document) { 54 NodeList nodeList=document.getElementsByTagName("book"); 55 for (int i = 0; i < nodeList.getLength(); i++) { 56 Node chilNode=nodeList.item(i); 57 String id=((Element)chilNode).getAttribute("id"); 58 if("b001".equals(id)) 59 { 60 Node parentNode=chilNode.getParentNode(); 61 parentNode.removeChild(chilNode); 62 } 63 } 64 } 65 66 /** 67 * 更新XML 68 * 1、獲得所有結點 69 * 2、獲得每一本書 70 * 3、獲得ID 71 * 4、更新 72 * *1、更新id 73 * *2、更新子節點 74 */ 75 private static void UpdateXml(Document document) { 76 NodeList nodeList=document.getElementsByTagName("book"); 77 for (int i = 0; i < nodeList.getLength(); i++) { 78 Node node=nodeList.item(i); 79 Element element=(Element) node; 80 String id=element.getAttribute("id"); 81 if("b001".equals(id)) 82 { 83 //element.setAttribute("id", "booooo"); //更新id值 84 NodeList childNode=element.getElementsByTagName("title"); 85 Element childElement=(Element) childNode.item(0);//childNode.getLength()=1 86 childElement.setTextContent("Java核心技術"); 87 } 88 } 89 } 90 91 /** 92 * 儲存xml 93 * 1、獲得持久化對象執行個體工廠 94 * 2、獲得持久化對象 95 * 3、將記憶體資料儲存到磁碟 96 * *源————將document對象封裝到Source 97 * *結果————將檔案路徑封裝到Result 98 * 4、儲存 99 */100 private static void Save(Document document)101 throws Exception {102 TransformerFactory factory=TransformerFactory.newInstance();103 Transformer transformer=factory.newTransformer();104 Source xmlSource=new DOMSource(document);105 javax.xml.transform.Result outputTarget=new StreamResult("books.jaxp.xml");106 107 transformer.transform(xmlSource, outputTarget);108 109 System.out.println("done");110 }111 112 /**113 * 擷取文檔114 * 1、獲得執行個體工廠115 * 2、獲得解析器116 * 3、獲得document117 */118 119 private static Document getDocument() throws ParserConfigurationException,120 SAXException, IOException {121 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();122 DocumentBuilder builder=factory.newDocumentBuilder();123 Document document=builder.parse("books.xml");124 return document;125 }
DOM 添加 / 更新 / 刪除 XML (CURD)