VOOZH about

URL: https://www.geeksforgeeks.org/java/xml-eventwriter-in-java-stax/

⇱ XML EventWriter in Java StAX - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

XML EventWriter in Java StAX

Last Updated : 25 Jul, 2022

Java StAX API is also called Java Streaming API for XML and it was introduced in Java 6. This is the most wanted API while considering DOM and SAX parsers.  StAX consists of cursor-based API and iterator-based API. In this article, let us see how to prepare an XML file in java using StAX Iterator-based API (XMLEventWriter). For creating an XML structure, we specifically need to see a few important methods of XMLEventWriter. XMLEventWriter is the top level interface for writing XML documents.

Methods

Description

void add(XMLEvent event)
 
This will start adding an event to the output stream. Adding START_ELEMENT will help to open a new namespace scope. Finishing with  END_ELEMENT the element is written and tags are closed.
void add(XMLEventReader reader)
 
In case an entire stream to an output stream needs to be added, this is used. On an iteration basis, it will work on the inputStream by calling next() and it will be over until hasNext() returns false. This is the most efficient way to add as it iterates and loop over all the events and call add to each event.
void close()
 
Finally, after constructing XML, this has to be called to free the resources that are associated.

Example

Let us see a sample program on how to create an XML file named "geekauthordetails.xml" by using XMLEventWriter. In the whole set of programs, to bring a structured output,

  • \n is used to make the data to be available on the new line
  • \t is used to display the tabbed data contents.

If not required, we can remove them. Moreover, now the current program produces the output in the file named "geekauthordetails.xml". If we want to display the output in the console, we need to uncomment certain parts of the code.

try {
 // This line creates the output in file. It has to be commented out
 XMLEventWriter xmlEventWriter = xmlOutputFactory
 .createXMLEventWriter(new FileOutputStream(geekAuthorFileName), "UTF-8");
 // If we want to see the output directly in the console, instead of writing to the file, we can write to the console
 // Just uncomment the below line and remove FileNotFoundException, we can achieve that
 // XMLEventWriter xmlEventWriter = xmlOutputFactory.createXMLEventWriter(System.out);
}

GeekAuthorDetailsStaxXMLWriter.java

Now, it has been set to write the output in the file itself. Hence we can able to get a file named "geekauthordetails.xml" mentioned in the location. Its contents are as follows. the image contains both styles if written to the console/if written to the file.

Output:

👁 Output
"geekauthordetails.xml" file contents as second image. First image represents console contents

Conclusion

Though both SAX and StAX are stream-based or event-oriented XML parsers, SAX uses a "push" model, whereas StAX uses a "pull" model. via SAX, XML cannot be written and there is no support for it. But only in StAX, using XMLEventWriter, writing XML is possible and it is illustrated in the above example program.

Comment
Article Tags:
Article Tags: