Saturday, June 6, 2015

Writing XML File With STAX Stream Parser

Here we try to learn how to use STAX Stream Parser in XML writing. We know that STAX stream parser is consuming lower memory and more efficient than STAX Event Parser . Now we follow this steps to create a XML file using STAX Stream Parser.

Step : 1
       Create XMLOutputFactory object.
          XMLOutputFactory xof = XMLOutputFactory.newFactory();

Step : 2 
        Create XMLStreamWriter object.

           XMLStreamWriter writer = xof.createXMLStreamWriter(new FileWriter("C:\\file.xml"));

 Step : 3
         Start Document
            writer.writeStartDocument();

         Start Element
           writer.writeStartElement("students");

         Add Attribute
            writer.writeAttribute("id", "1");

         Add Character in XML Node
            writer.writeCharacters("TestLaststName");

        End Element
             writer.writeEndElement();

        End Document
             writer.writeEndDocument();

Step : 4
      Flush XMLStreamWriter object and close this object.
        
          writer.flush();
          writer.close();


Complete Code Is Given Below :


package STAXParser;

import com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;

public class STAXStreamWriter {

    public static void main(String[] args) throws IOException, XMLStreamException, TransformerConfigurationException, TransformerException {
        /**
         * Create XMLOutputFactory Object.
         */

        XMLOutputFactory xof = XMLOutputFactory.newFactory();

        /**
         * Create XMLStreamWriter Object.
         */

        XMLStreamWriter writer = xof.createXMLStreamWriter(new FileWriter("C:\\file.xml"));
//        XMLStreamWriter writer = xof.createXMLStreamWriter(new StreamResult(System.out));
        /**
         * This is created for formatting.
         */

        writer = new IndentingXMLStreamWriter(writer);

        /**
         * Handling Start Document,Start Element,Add Attribute,
         * Add Character,EndDocument and EndElement.
         */

        writer.writeStartDocument();
       
        writer.writeStartElement("students");
       
        writer.writeStartElement("student");
        writer.writeAttribute("id", "1");

        writer.writeStartElement("fname");
        writer.writeCharacters("TestFirstName");
        writer.writeEndElement();

        writer.writeStartElement("lname");
        writer.writeCharacters("TestLaststName");
        writer.writeEndElement();

        writer.writeStartElement("section");
        writer.writeAttribute("rollno", "1");
        writer.writeCharacters("A");
        writer.writeEndElement();
       
        writer.writeEndElement();
        writer.writeEndElement();
        writer.writeEndDocument();

        /**
         * Flush XMLStreamWriter Object and Close.
         */

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


OutPut :
 

<?xml version="1.0" encoding="UTF-8"?>
<studens>
    <student id="1">
        <fname>TestFirstName</fname>
        <lname>TestLastName</lname>
        <section rollno="1">A</section>
    </student>
</studens>

No comments:

Post a Comment