zoukankan      html  css  js  c++  java
  • 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();
        }
    }
  • 相关阅读:
    把一元可以分解成几个1毛,2毛,5毛?
    记录集导出到Excel方法
    MySQL数据库加密与解密:
    运行时错误'430': 类不支持自动化或不支持期望的接口。New ADODB.Connection问题
    instrrev 和instr 区别vb
    解决用 VB 中用 ADO 访问 数据库时 SQL 查询处理 Null 值的问题( 使用 iff(isNull(字段), 为空时的值,不为空时的值) 来处理)
    Mysql SQL CAST()函数
    MySQL CAST与CONVERT 函数的用法
    一些网页链接
    git上传时出现ERROR: Repository not found.的解决办法
  • 原文地址:https://www.cnblogs.com/nayitian/p/2867241.html
Copyright © 2011-2022 走看看