一、可擴展標記語言xml:Extensible Markup Language
1、XML的作用:1)統一數據傳輸的格式。2)做小型數據庫[文件型數據庫]。3)做配置文件 .ini .propertity .xml .cfg
2、XML文件的基本格式:
標簽:分為雙標簽和單標簽,雙標簽的開頭和結尾標簽名必須一致,大小寫一樣,/ 開頭的是結尾標簽,單標簽必須在 > 前加上 / 來結尾,單標簽中不能放文本。
屬性:在開始標簽中定義一些名值對,值一定是字符串
3、XML文件的基本構成:
1)在第一行是XML聲明 <?xml version="1.0" encoding="UTF-8" ?>
2)必須且只能有一對根標簽
3)標簽可以一層一層嵌
二、寫一個簡單的xml文檔stus.xml然后用Dom進行解析。
DOM : Document Object Model 文檔對象模型
DOM解析的基本思路:將整個XML文件一次性讀入內存,將整個XML看做一棵文檔樹,XML中的每一個標簽,屬性,文本都看做是樹上的一個結點,然后可以對結點進行增刪改查的操作。
已經編輯好的stus.xml文檔
- <?xml version="1.0" encoding="UTF-8"?>
- <stus Class="1401" >
- <stu num="01">
- <name>張三</name>
- <age>19</age>
- <sex>男</sex>
- </stu>
- <stu num="02">
- <name>李四</name>
- <age>20</age>
- <sex>女</sex>
- </stu>
- <stu num="03">
- <name>王五</name>
- <age>21</age>
- <sex>男</sex>
- </stu>
- </stus>
三、開始解析
創建解析工廠
- // 得到解析工廠對象
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- // 生產一個解析器對象
- DocumentBuilder builder = factory.newDocumentBuilder();
- // 開始解析xml文件,得到的解析結果是一個Document對象,Document對象叫做文檔樹對象
- Document document = builder.parse("stus.xml");
1、增加節點
基本思路:首先創建一個新的元素節點,將元素節點追加到根節點后面,設置其節點屬性。創建標簽,設置標簽文本內容,最后將新標簽添加到新的元素節點中。
代碼:
- // 創建一個新的元素節點
- Element stu = dom.createElement("stu");
- // 將元素節點追加到根節點后面
- root.appendChild(stu);
- // 設置節點屬性
- stu.setAttribute("num", "04");
- // 創建標簽
- Element name = dom.createElement("name");
- Element age = dom.createElement("age");
- Element sex = dom.createElement("sex");
- // 設置標簽文本內容
- name.setTextContent("趙六");
- age.setTextContent("19");
- sex.setTextContent("女");
- // 把標簽添加到新的元素節點stu中
- stu.appendChild(name);
- stu.appendChild(age);
- stu.appendChild(sex);
2、刪除節點
基本思路:獲得要刪除的節點,然后得到節點的屬性值,與要刪除的節點的屬性值進行比較,如果該屬性值對應的節點存在則移除該節點。
代碼:
- // 獲得根節點
- Element root = (Element) dom.getFirstChild();
- // 獲得所有stu節點
- NodeList list = dom.getElementsByTagName("stu");
- for (int i = 0; i < list.getLength(); i++) {
- Node node = list.item(i);
- if (node instanceof Element) {
- Element e = (Element) node;
- // 得到節點的屬性值,與要刪除的結點的屬性值進行比較,然后移除該屬性值對應的結點
- String num = e.getAttribute("num");
- if (num.equals("02")) {
- root.removeChild(e);
- break;
- }
- }
- }
3、修改節點
基本思路:獲得要修改的節點,修改其屬性值,然后獲得該節點下的標簽,修改標簽中的文本內容。
代碼:
- // 修改節點屬性
- for (int j = 0; j < list.getLength(); j++) {
- Node no = list.item(j);
- if (no instanceof Element) {
- Element el = (Element) no;
- String n = el.getAttribute("num");
- if (n.contains("01")) {
- el.setAttribute("num", "05");
- // 修改標簽值
- NodeList li = el.getChildNodes();
- for (int x = 0; x < li.getLength(); x++) {
- Node d = li.item(x);
- if (d instanceof Element) {
- Element ee = (Element) d;
- String noN = ee.getNodeName();
- if (noN.equals("name")) {
- ee.setTextContent("小白");
- } else if (noN.equals("age")) {
- ee.setTextContent("11");
- } else if (noN.equals("sex")) {
- ee.setTextContent("男");
- }
- }
- }
- }
4、查找節點
基本思路:獲得所有的節點,用 需要查找的結點的屬性值與所有節點進行比較,如果該節點存在,就打印該節點的屬性值及其節點下標簽的內容。
代碼:
- for (int j = 0; j < list.getLength(); j++) {
- Node no = list.item(j);
- if (no instanceof Element) {
- Element el = (Element) no;
- String n = el.getAttribute("num");
- //查找節點,顯示其屬性值及標簽內容
- if(n.equals("03")){
- System.out.println(no.getNodeName()+"\t"+n+no.getTextContent());
- }
- }
- }
4、保存修改后的xml文檔
基本思路:先將內存中的Document對象寫到xml文件中,然后將整個Document對象作為要寫入xml文件的數據源,最后將數據源寫入目標文件。
代碼:
- // 將內存中的Document對象寫到xml文件中
- TransformerFactory tf = TransformerFactory.newInstance();
- Transformer former = tf.newTransformer();
- former.setParameter("version", "1.0");
- former.setParameter("encoding", "GBK");
- // 將整個Document對象作為要寫入xml文件的數據源
- DOMSource xmlSource = new DOMSource(dom);
- // 要寫入的目標文件
- StreamResult outputTarget = new
- StreamResult(new File("F:\\stus2.xml"));
- former.transform(xmlSource, outputTarget);
http://www.99inf.com/jinrong/yhbx/1071334.html
http://www.99inf.com/jinrong/yhbx/1071318.html
http://www.99inf.com/jinrong/yhbx/1071308.html
http://www.99inf.com/jinrong/yhbx/1071302.html
http://www.99inf.com/jinrong/yhbx/1071288.html
http://www.xianhuo8.cn/bbs/thread-156595-1-1.html
http://www.xianhuo8.cn/bbs/thread-156610-1-1.html
http://www.diyizby.com/bbs/thread-4275199-1-1.html