欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)XML增加元素操作簡(jiǎn)單示例

 更新時(shí)間:2017年02月06日 12:04:20   作者:zzcchunter  
這篇文章主要介紹了java實(shí)現(xiàn)XML增加元素操作,結(jié)合簡(jiǎn)單實(shí)例形式分析了java針對(duì)xml格式數(shù)據(jù)的讀取、遍歷、創(chuàng)建等操作技巧,需要的朋友可以參考下

本文實(shí)例講述了java實(shí)現(xiàn)XML增加元素操作。分享給大家供大家參考,具體如下:

package Day01;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
public class CRUDDEMO {
  /*public void addElement() throws Exception{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File ("src/Day01/Book.xml"));
    Element newEle = doc.createElement("作者");
    newEle.setTextContent("ZC");
    Node nod = doc.getElementsByTagName("書(shū)").item(0);
    nod.appendChild(newEle);
    Source sour = new DOMSource(doc);
    Result result = new StreamResult (new FileOutputStream("src/Day01/Book.xml"));
    write (sour, result);
  }*/
  public void addElement2() throws Exception{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //建立工廠
    DocumentBuilder builder = factory.newDocumentBuilder(); //拿到builder
    Document doc = builder.parse(new File ("src/Day01/Book.xml")); //獲得document,這是終極目的
    Element newEle = doc.createElement("作者");// 創(chuàng)建新元素/標(biāo)簽
    newEle.setTextContent("ZC"); //給元素設(shè)置內(nèi)容 <作者>ZC</作者>
    Node nod = doc.getElementsByTagName("書(shū)名").item(0); //通過(guò)nodelist的item()方法獲得具體節(jié)點(diǎn)
    /**
     * 在具體節(jié)點(diǎn)插入元素用 節(jié)點(diǎn).insertBefore方法
     * 第一個(gè)參數(shù)是要插入的新節(jié)點(diǎn),第二個(gè)是插入的位置
     */
    nod.insertBefore(newEle, doc.getElementsByTagName("書(shū)名").item(0));
    /**
     * DOMSource(Node n)
     * 注意 element是Node的一個(gè)子類,所以可以把doc放入構(gòu)造函數(shù)
     *
     *
     */
    Source sour = new DOMSource(doc);
    Result result = new StreamResult (new FileOutputStream("src/Day01/Book.xml"));
    write (sour, result);
  }
  public void write(Source source,Result result) {
    TransformerFactory tffactory = TransformerFactory.newInstance();
    Transformer tr;
    try {
      tr = tffactory.newTransformer();
      tr.transform(source, result);
    } catch (TransformerConfigurationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (TransformerException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  public static void main(String[] args) throws Exception {
    CRUDDEMO cr = new CRUDDEMO();
    cr.addElement2();
  }
}

修改前的XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<書(shū)架>
  <書(shū)>
    <書(shū)名>Thinking in Java</書(shū)名>
    <作者>Eric</作者>
    <售價(jià)>$34</售價(jià)>
  </書(shū)>
</書(shū)架>

修改后的XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<書(shū)架>
  <書(shū)>
    <作者>ZC</作者>
    <書(shū)名>Thinking in Java</書(shū)名>
    <作者>Eric</作者>
    <售價(jià)>$34</售價(jià)>
  </書(shū)>
</書(shū)架>

PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:

在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat

XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論