Saturday, June 6, 2015

Writing XML File Using STAX Event Parser

Using STAX parser is another way to write XML file. As we know STAX have two type of parsing technique one is Event or Iterator and another one is Stream or Cursor type,here STAX have same two types to write XML file.

Here we take a look on STAX event Parser, later we will know the another one.

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


Step 2 :
      Create XMLEventWriter object.
         XMLEventWriter xew=outFactory.createXMLEventWriter(new FileWriter("File Name"));

Step 3:
      Create XMLEventFactoryobject.
         XMLEventFactory evntFactory=XMLEventFactory.newFactory();

Step 4 :
      Start Document
         xew.add(evntFactory.createStartDocument());
     
      StartElement
        xew.add(evntFactory.createStartElement("", "", "Element Name"));   

      Add Attribute
        xew.add(evntFactory.createAttribute("Attribute Name", "Attribute Value"));

      Add Character
        xew.add(evntFactory.createCharacters("XML Node Character Value"));
 
     Close Element
        xew.add(evntFactory.createEndElement("", "", "Element Name Which we want to close"));
     
     Close Document 
        xew.add(evntFactory.createEndDocument());

Step 5 :
     Flush the writer object.
        xew.flush();

Step 6 :
     Close writer object.
        xew.close();
       

Complete Code Given Below :

package STAXParser;

import java.io.FileWriter;
import java.io.IOException;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;

public class STAXEventWriter {

    public static void main(String[] args) throws IOException, XMLStreamException {
        

        /**
         * Create XMLOutputFactory object.
         */

 
        XMLOutputFactory outFactory=XMLOutputFactory.newFactory();
        

        /**
         * Create XMLEventWriter object.
         */

 
        XMLEventWriter xew=outFactory.createXMLEventWriter(new FileWriter("C:\\file.xml"));
        //XMLEventWriter xew=outFactory.createXMLEventWriter(System.out);


        /**
         * Create XMLEventFactory object.
         */

        XMLEventFactory evntFactory=XMLEventFactory.newFactory();
       
        /**
         * This are created to decorate XML file.
         */

 
        XMLEvent end = evntFactory.createDTD("\n");
        XMLEvent tab = evntFactory.createDTD("\t");
       
        /**
         * Handling Stat Document, CreateElement, Add Attribute
         * Create Characters, Close Element and close Document.
         */

        xew.add(evntFactory.createStartDocument());
        xew.add(end);
       
        xew.add(evntFactory.createStartElement("", "", "studens"));
        xew.add(end);
        xew.add(tab);
       
        xew.add(evntFactory.createStartElement("", "", "student"));
        xew.add(evntFactory.createAttribute("id", "1"));
        xew.add(end);
        xew.add(tab);
        xew.add(tab);
       
        xew.add(evntFactory.createStartElement("", "", "fname"));
        xew.add(evntFactory.createCharacters("TestFirstName"));
        xew.add(evntFactory.createEndElement("", "", "fname"));
        xew.add(end);
        xew.add(tab);
        xew.add(tab);
       
        xew.add(evntFactory.createStartElement("", "", "lname"));
        xew.add(evntFactory.createCharacters("TestLastName"));
        xew.add(evntFactory.createEndElement("", "", "lname"));
        xew.add(end);
        xew.add(tab);
        xew.add(tab);
       
        xew.add(evntFactory.createStartElement("", "", "section"));
        xew.add(evntFactory.createAttribute("rollno", "1"));
        xew.add(evntFactory.createCharacters("A"));
        xew.add(evntFactory.createEndElement("", "", "section"));
        xew.add(end);
        xew.add(tab);
       
        xew.add(evntFactory.createEndElement("", "", "student"));
        xew.add(end);
       
        xew.add(evntFactory.createEndElement("", "", "students"));
        xew.add(evntFactory.createEndDocument());
       
        /**
         * Flush writer object and Close this object.
         */

        xew.flush();
        xew.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>


1 comment: