<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    甜咖啡

    我的IT空間

    DOM生成和解析XML文檔

    1.DOM生成和解析XML文檔


    為 XML 文檔的已解析版本定義了一組接口。解析器讀入整個文檔,然后構建一個駐留內存的樹結構,然后代碼就可以使用 DOM
    接口來操作這個樹結構。優點:整個文檔樹在內存中,便于操作;支持刪除、修改、重新排列等多種功能;缺點:將整個文檔調入內存(包括無用的節點),浪費時間和空間;使用場合:一旦解析了文檔還需多次訪問這些數據;硬件資源充足(內存、CPU)。

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.PrintWriter;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerConfigurationException;
    import javax.xml.transform.TransformerException;
    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.xml.sax.SAXException;
    /**
    *
    * @author hongliang.dinghl
    * DOM生成與解析XML文檔
    */
    public class DomDemo implements XmlDocument {
    private Document document;
    private String fileName;
    public void init() {
    try {
    DocumentBuilderFactory factory = DocumentBuilderFactory
    .newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    this.document = builder.newDocument();
    } catch (ParserConfigurationException e) {
    System.out.println(e.getMessage());
    }
    }
    public void createXml(String fileName) {
    Element root = this.document.createElement("employees");
    this.document.appendChild(root);
    Element employee = this.document.createElement("employee");
    Element name = this.document.createElement("name");
    name.appendChild(this.document.createTextNode("丁宏亮"));
    employee.appendChild(name);
    Element sex = this.document.createElement("sex");
    sex.appendChild(this.document.createTextNode("m"));
    employee.appendChild(sex);
    Element age = this.document.createElement("age");
    age.appendChild(this.document.createTextNode("30"));
    employee.appendChild(age);
    root.appendChild(employee);
    TransformerFactory tf = TransformerFactory.newInstance();
    try {
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
    StreamResult result = new StreamResult(pw);
    transformer.transform(source, result);
    System.out.println("生成XML文件成功!");
    } catch (TransformerConfigurationException e) {
    System.out.println(e.getMessage());
    } catch (IllegalArgumentException e) {
    System.out.println(e.getMessage());
    } catch (FileNotFoundException e) {
    System.out.println(e.getMessage());
    } catch (TransformerException e) {
    System.out.println(e.getMessage());
    }
    }
    public void parserXml(String fileName) {
    try {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(fileName);
    NodeList employees = document.getChildNodes();
    for (int i = 0; i < employees.getLength(); i++) {
    Node employee = employees.item(i);
    NodeList employeeInfo = employee.getChildNodes();
    for (int j = 0; j < employeeInfo.getLength(); j++) {
    Node node = employeeInfo.item(j);
    NodeList employeeMeta = node.getChildNodes();
    for (int k = 0; k < employeeMeta.getLength(); k++) {
    System.out.println(employeeMeta.item(k).getNodeName()
    + ":" + employeeMeta.item(k).getTextContent());
    }
    }
    }
    System.out.println("解析完畢");
    } catch (FileNotFoundException e) {
    System.out.println(e.getMessage());
    } catch (ParserConfigurationException e) {
    System.out.println(e.getMessage());
    } catch (SAXException e) {
    System.out.println(e.getMessage());
    } catch (IOException e) {
    System.out.println(e.getMessage());
    }
    }
    }

    posted on 2011-07-19 16:12 甜咖啡 閱讀(299) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    導航

    <2011年7月>
    262728293012
    3456789
    10111213141516
    17181920212223
    24252627282930
    31123456

    統計

    常用鏈接

    留言簿(1)

    我參與的團隊

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲精品国产免费| 亚洲精品无码高潮喷水在线| 亚洲AV无码不卡无码| 久久久久久av无码免费看大片| 四虎亚洲国产成人久久精品| 黄人成a动漫片免费网站| 免费观看日本污污ww网站一区| 亚洲国产成人久久精品软件 | 在线看片免费人成视久网| 亚洲gv猛男gv无码男同短文| 精品四虎免费观看国产高清午夜| 亚洲国产精品无码专区在线观看| a级毛片在线免费| 久久亚洲AV成人无码国产| 91久久精品国产免费直播| 色天使亚洲综合在线观看| 国内自产拍自a免费毛片| 免费亚洲视频在线观看| 在线日韩日本国产亚洲| 久久久久久AV无码免费网站| 亚洲国语在线视频手机在线| 成人永久免费福利视频网站| 一级特黄录像视频免费| 久久精品亚洲综合专区| 日韩国产免费一区二区三区| 亚洲AV永久无码精品一福利| 亚洲AV蜜桃永久无码精品| 国产色无码精品视频免费| 亚洲精品福利网泷泽萝拉| 日本高清免费中文字幕不卡| 窝窝影视午夜看片免费| 久久国产亚洲观看| 免费一本色道久久一区| 日韩精品无码永久免费网站| 亚洲AV无码专区电影在线观看| 欧洲乱码伦视频免费| 牛牛在线精品免费视频观看| 亚洲av中文无码乱人伦在线播放 | 永久黄网站色视频免费直播| a级毛片免费网站| 亚洲国产av美女网站|