DOM 添加 / 更新 / 刪除 XML (CURD)

來源:互聯網
上載者:User

標籤: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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.