Java 流式Xml读取 写入 StAX

2 篇文章 0 订阅

http://docs.oracle.com/javase/tutorial/jaxp/stax/index.html

StAX is the latest API in the JAXP family, and provides an alternative to SAX, DOM, TrAX, and DOM for developers looking to do high-performance stream filtering, processing, and modification, particularly with low memory and limited extensibility requirements.

package staxdemo;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
public class Demo {

	public static void main(String[] args) throws FileNotFoundException, XMLStreamException {
		writeDemo();
		
	}
	
	

	private static void writeDemo() throws FactoryConfigurationError,
			XMLStreamException, FileNotFoundException {
		XMLOutputFactory factory = XMLOutputFactory.newInstance();
		XMLStreamWriter writer =  new com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter(factory.createXMLStreamWriter(
				new FileOutputStream("demo.xml")));
		writer.writeStartDocument();
		writer.writeStartElement("group");
		writer.writeAttribute("name", "mygroup");
		writer.writeStartElement("student");
		writer.writeAttribute("studentname", "张三");
		writer.writeEndElement();
		
		writer.writeStartElement("student");
		writer.writeAttribute("studentname", "李四");
		writer.writeEndElement();
		
		writer.writeEndElement();
		writer.writeEndDocument();
		
		writer.flush();
		writer.close();
	}

}


<?xml version="1.0" ?>
<group name="mygroup">
  <student studentname="张三"></student>
  <student studentname="李四"></student>
</group>



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值