Java 流式Xml读取 写入 StAX

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>



你可能感兴趣的:(Java 流式Xml读取 写入 StAX)