http://tutorials.jenkov.com/java-xml/stax-xmlstreamwriter.html에서 가져옴


The XMLStreamWriter class in the Java StAX API allows you to write XML events (elements, attributes etc.) either to a Writer, an OutputStream, or a Result (special JAXP object).

Here is a simple example that writes a series of events to disk, using a FileWriter:

XMLOutputFactory factory      = XMLOutputFactory.newInstance();

 try {
     XMLStreamWriter writer = factory.createXMLStreamWriter(
             new FileWriter("data\\output2.xml"));

     writer.writeStartDocument();
     writer.writeStartElement("document");
     writer.writeStartElement("data");
     writer.writeAttribute("name", "value");
     writer.writeEndElement();
     writer.writeEndElement();
     writer.writeEndDocument();

     writer.flush();
     writer.close();

 } catch (XMLStreamException e) {
     e.printStackTrace();
 } catch (IOException e) {
     e.printStackTrace();
 }

The result of executing this code is the following XML file (line breaks inserted for readability):

<?xml version='1.0' encoding='utf-8'?>
<document><data name="value"></data></document>

'밤을 지새다 > Java' 카테고리의 다른 글

Java PriorityQueue  (0) 2012.05.07
Java StAX Event  (0) 2012.05.01
Java StAX: XMLStreamReader - The Cursor API  (0) 2012.04.30
Java StAX: XMLEventWriter - The Iterator Writer API  (0) 2012.04.30
Java StAX: XMLEventReader - The Iterator API  (0) 2012.04.30

+ Recent posts