天天看點

XML 使用介紹

1-XML:可擴充的标記語言,類似HTML。用來傳輸資料。與此類似的還有JSON格式的。自定義标簽。

              XML的解析常用dom4j包。

2-XML缺點:檔案大,格式複雜,解析方式多種可能不一緻,傳輸帶寬要求高等。具體和Json的對比,html

3-詳細的基礎知識 見-http://www.ibm.com/developerworks/cn/xml/x-newxml/

XML的解析 見下面代碼:

package com;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

public class TestXml {
  /**
   *<famliy city="Beijing">  
        <fumu name="luxury" age="24"></student>  
        <fumu name="chenchen" age="21"></student>  
        <haizi name="lucy" age="11"></teacher>  
        <haizi name="tom" age="8"></teacher>  
      </famliy>  
   */
  @Test
  public void test1(){
    /**
     * 解析  XML檔案
     */
    File xml = new File("R:\\famliy.xml");
    System.out.println("xml路徑 : "+xml.getPath());
    if(xml.exists()){
      SAXReader reader = new SAXReader();
      try {
        //讀取xml檔案 到 Document(文檔)
        Document document = reader.read(xml);
        //把 Document 轉化為 字元串--列印出 xml檔案 内容
        System.out.println("#内容#\r"+document.asXML());
        System.out.println("======================================");
        //得到 root節點
        Element root = document.getRootElement();
        //獲得  root節點的 city屬性
        System.out.println(root.getName()+" 節點[city-屬性]: "+root.attributeValue("city"));
        
        System.out.println("======================================");
        //iterator 周遊 解析root根節點下的 fumu子節點
        System.out.println("解析 fumu節點");
        for(Iterator iterator=root.elementIterator("fumu");iterator.hasNext();){
          Element e = (Element) iterator.next();
          System.out.println(e.getName()+" name-"+e.attributeValue("name")+" ; age-"+e.attributeValue("age"));
        }
        System.out.println("======================================");
        //iterator 周遊 解析root根節點下的  haizi子節點
        System.out.println("解析 haizi節點");
        for(Iterator iterator=root.elementIterator("haizi");iterator.hasNext();){
          Element e = (Element) iterator.next();
          System.out.println(e.getName()+" name-"+e.attributeValue("name")+" ; age-"+e.attributeValue("age"));
        }
        
      } catch (DocumentException e) {
        e.printStackTrace();
      }
      
    }else{
      System.out.println("檔案 不存在...");
    }
  }
  
  
  @Test
  public void test2(){
    /**
     * XML 和  字元串 
     */
    String str = "<so><head><body><gg name='mm' age='10'></gg></body></head></so>";
    try {
      //将 字元串 str 轉為  XML
      Document document = DocumentHelper.parseText(str);
      System.out.println(document);
      System.out.println(document.asXML());
      System.out.println(document.getRootElement().getName());
      
    } catch (DocumentException e) {
      e.printStackTrace();
    }
  }
  
  
  /**
   * 建立 Document
   * 添加 根節點+子節點
   * 給 子節點 添加值
   * 将Docuemnt 寫入  檔案.xml
   * @throws IOException
   */
  @Test
  public void test3() throws IOException{
    //建立 Document對象
    Document document = DocumentHelper.createDocument();
    //添加  root 根節點
    Element root = document.addElement("root");
    //在 root 下 添加 子節點 father
    Element father = root.addElement("childen");
    //給 father 添加值
    father.setText("好有愛哦");
    System.out.println(document.asXML());
    FileOutputStream fos = new FileOutputStream("R:\\ceshixml.xml",true);
    fos.write(document.asXML().getBytes());
    
    
  }
  
  
}