Monday 5 July 2021

Create Xml Using the Stax Processor

StAX is a standard XML processing API that allows you to stream XML data from and to your application.


This API is better than the DOM parser in the case of Performance. It does not load the whole document to the memory like the DOM parser. SAX is a push API where else Stax is the pull API. Using the SAX for the creation of the document is not recommended.


The Below code is used to generate the below XML.



package com.searchendeca.main;

import java.io.StringWriter;


import javax.xml.stream.XMLOutputFactory;

import javax.xml.stream.XMLStreamException;

import javax.xml.stream.XMLStreamWriter;


public class StaxParserMain {


private void createStudentElements(XMLStreamWriter xmlStreamWriter) {

try {

createStreamWriter("FirstName", "Syed", xmlStreamWriter);

createStreamWriter("LastName", "Ghouse", xmlStreamWriter);

createStreamWriter("City", "Salem", xmlStreamWriter);

} catch (XMLStreamException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

private void createStreamWriter(String key, Object value, XMLStreamWriter xmlStreamWriter)

throws XMLStreamException {

if (value != null && value != "") {

if (value instanceof String) {

xmlStreamWriter.writeStartElement(key);

xmlStreamWriter.writeCharacters((String) value);

xmlStreamWriter.writeEndElement();

}

} else {

xmlStreamWriter.writeEmptyElement(key);

}


}


public static void main(String args[]) {

StringWriter stringwriter = new StringWriter();

XMLOutputFactory xmloutputfactory = XMLOutputFactory.newInstance();

StaxParserMain stax = new StaxParserMain();

try {

XMLStreamWriter xmlStreamWriter = xmloutputfactory.createXMLStreamWriter(stringwriter);

xmlStreamWriter.writeStartDocument();

xmlStreamWriter.writeStartElement("Student");

stax.createStudentElements(xmlStreamWriter);

xmlStreamWriter.writeEndElement();

xmlStreamWriter.writeEndDocument();

xmlStreamWriter.flush();

xmlStreamWriter.close();

String xmlString= stringwriter.getBuffer().toString();

System.out.println(xmlString);


} catch (XMLStreamException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}


The whole project can be found in Github here.


Happy Learning !!!

No comments:
Write comments