Java:XML篇,使用SAX写入XML数据

1. 描述

参考:http://blog.csdn.net/hairongtian/article/details/7525292,Java中用SAX解析XML,具体包括读写,这篇是写入示范。

2. 示范代码

package com.clzhang.sample.xml;

import java.io.*;
import java.util.*;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.xml.sax.SAXException;

// SAX写入XML测试
// 参考:http://blog.csdn.net/hairongtian/article/details/7525292
public class SAXWriteTest {
    // 句柄
    private TransformerHandler handler = null;
    // 输出流
    private OutputStream outStream = null;
    // 根节点
    private String rootElement;

    public SAXWriteTest(String fileName, String rootElement) throws Exception {
        this.rootElement = rootElement;
        
        // 创建句柄,并设置初始参数
        SAXTransformerFactory fac = (SAXTransformerFactory) SAXTransformerFactory
                .newInstance();
        handler = fac.newTransformerHandler();
        Transformer transformer = handler.getTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

        outStream = new FileOutputStream(fileName);
        handler.setResult(new StreamResult(outStream));
        
        System.out.println("初始化成功");
    }

    public void start() throws Exception {
        handler.startDocument();
        handler.startElement("", "", rootElement, null);
        System.out.println("文档开始...");
    }
    
    // 这个函数是最重要的,它可以控制各个细节:是否写入属性值,文本值又是多少等等
    public void write(HashMap<String, String> map, String element)
            throws SAXException {
        handler.startElement("", "", element, null);
        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            String value = map.get(key);
            handler.startElement("", "", key, null);
            handler.characters(value.toCharArray(), 0, value.length());
            handler.endElement("", "", key);
        }
        handler.endElement("", "", element);
        System.out.println("写入元素成功!");
    }

    public void end() throws Exception {
        handler.endElement("", "", rootElement);
        handler.endDocument();
        outStream.close();
        System.out.println("文档结束!");
    }

    public static void main(String[] args) throws Exception {
        SAXWriteTest xml = new SAXWriteTest("student.xml", "students");
        xml.start();
        
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", "20050505");
        map.put("name", "zhaobenshan");
        map.put("age", "21");
        map.put("classes", "Act051");
        xml.write(map, "student");

        map = new HashMap<String, String>();
        map.put("id", "20050506");
        map.put("name", "songdandan");
        map.put("age", "20");
        map.put("classes", "Act052");
        xml.write(map, "student");

        map = new HashMap<String, String>();
        map.put("id", "20050507");
        map.put("name", "fanchushi");
        map.put("age", "21");
        map.put("classes", "Act051");
        xml.write(map, "student");

        xml.end();
    }
}

 

posted @ 2013-01-19 08:16  那些年的事儿  阅读(1142)  评论(0编辑  收藏  举报