jaxp dom-appendChild 某一個標籤下建立 有內容無屬性的標籤

來源:互聯網
上載者:User

標籤:oms   鍛煉   相對   image   技術   dom   lips   工廠   jaxp   

禮悟:
     好好學習多思考,尊師重道存感恩。葉見尋根三二一,江河湖海同一體。
          虛懷若穀良心主,願行無悔給最苦。讀書鍛煉強身心,誠勸且行且珍惜。

 

 

    jdk:1.8
    xml:1.0
     os:windows7 x64
    ide:MyEclipse 2017



原本的XML

<?xml version="1.0" encoding="UTF-8"?><shopping><book><name id="0001">靈樞</name><price discount="90" originalPrice="100">90</price><count>3</count><comment>預設好評</comment></book><book><name id="0002">素問</name><price discount="90" originalPrice="200">180</price><count>5</count><comment>預設好評</comment></book><book><name id="0003">傷寒雜病論</name><price discount="90" originalPrice="300">270</price><count>7</count><comment>預設好評</comment></book></shopping>

 

 

代碼:

 

package jizuiku.xml_E;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.Text;/** * jaxp 在某一個標籤下建立 有內容無屬性的標籤 *  * @author 給最苦 * @version V17.10.10 */public class JaxpDemo {public static void main(String[] args) throws Exception{// 建立解析器工廠DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();// 建立解析器DocumentBuilder builder = builderFactory.newDocumentBuilder();// 解析xml返回document// uri為相對路徑String uri = "src\\jizuiku\\xml_E\\shopping.xml";// Document -> import org.w3c.dom.Document;Document document = builder.parse(uri);// 得到所有的book元素NodeList bookNodeList = document.getElementsByTagName("book");// 在第一個book標籤下,建立<sales>1000</sales>Node firstBookNode = bookNodeList.item(0);// 建立標籤Element salesNode = document.createElement("sales");Text salesNodeText = document.createTextNode("1000");// 把文本加到標籤下salesNode.appendChild(salesNodeText);// 把標籤加到標籤下firstBookNode.appendChild(salesNode);// 回寫操作,感覺好複雜呀TransformerFactory transformerFactory = TransformerFactory.newInstance();Transformer transformer = transformerFactory.newTransformer();transformer.transform(new DOMSource(document), new StreamResult(uri));}}

 

 

經過修改的XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?><shopping><book><name id="0001">靈樞</name><price discount="90" originalPrice="100">90</price><count>3</count><comment>預設好評</comment><sales>1000</sales></book><book><name id="0002">素問</name><price discount="90" originalPrice="200">180</price><count>5</count><comment>預設好評</comment></book><book><name id="0003">傷寒雜病論</name><price discount="90" originalPrice="300">270</price><count>7</count><comment>預設好評</comment></book></shopping>

 

雖然修改成功了,但是xml檔案的格式改變了

 

 

xml優秀,值得學習。
學習資源:itcast和itheima視頻庫。如果您有公開的資源,可以分享給我的話,用您的資源學習也可以。
博文是觀看視頻後,融入思考寫成的。博文好,是老師講得好。博文壞,是 給最苦 沒認真。

jaxp dom-appendChild 某一個標籤下建立 有內容無屬性的標籤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.