XML Output Factory in Java StAX
Last Updated :
29 Oct, 2021
StAX provides various classes to create XML stream readers, writers, and events by using the XMLInputFactory, XMLOutputFactory, and XMLEventFactory classes. In this article we’re going to study about XML Output Factory . The class javax.xml.stream.XMLOutputFactory is a root component of the Java StAX API. From this class you can create both an XMLStreamWriter and an XMLEventWriter.
Note: XMLOutputFactory hold up only one property, javax.xml.stream.isRepairingNamespaces. This property is required, and its purpose is to create default prefixes and associate them with Namespace URIs.
Procedure:
- Create an new abstract class of XMLOutputFactory using newInstance() method.
- Create an instance of XMLStreamWriter or XMLEventWriter using XMLOutputFactory instance.
- Writing the header of the XML
- Create statements
- Add elements so that we can addon attributes, namespaces.
- Flush and close opened elements
- Add try and catch block
They are as illustrated below as follows:
Step 1: Create new instance of abstract class XMLOutputFactory by calling the newInstance() method
XMLOutputFactory factory = XMLOutputFactory.newInstance();
Step 2: Using this instance, Create an XMLStreamWriter and an XMLEventWriter
XMLEventWriter eventWriter = factory.createXMLEventWriter(new FileWriter("data\\gfg.xml"));
XMLStreamWriter streamWriter = factory.createXMLStreamWriter(new FileWriter("data\\gfg.xml"));
Step 3: Write the header of the XML and proceed to create start elements.
XMLEvent event = eventFactory.createStartDocument();
event = eventFactory.createStartElement("GFG", "https://www.geeksforgeeks.org/", "document");
Step 4: After adding elements we can add attributes, namespace.
event = eventFactory.createNamespace("GeeksforGeeks-practice", "https://practice.geeksforgeeks.org/");
writer.add(event);
event = eventFactory.createAttribute("attribute", "value");
writer.add(event);
Step 5: Flush and close opened elements.
writer.flush();
writer.close();
Step 6: Add try and catch block.
try
{
--code--
}
catch (XMLStreamException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
Example of XMLOutputFactory
Java
import java.io.*;
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;
class GFG {
public static void main(String[] args)
{
XMLOutputFactory factory= XMLOutputFactory.newInstance();
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
try {
XMLEventWriter writer = factory.createXMLEventWriter( new FileWriter( "D:\\gfg_OutputFactory.xml" ));
XMLEvent event = eventFactory.createStartDocument();
writer.add(event);
writer.add(event);
writer.add(event);
event = eventFactory.createAttribute( "attribute" , "GFG" );
writer.add(event);
writer.add(event);
writer.flush();
writer.close();
}
catch (XMLStreamException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
|
Output:
<?xml version='1.0' encoding='UTF-8'?>
<GFG:document xmlns:GeeksforGeeks-practice="https://practice.geeksforgeeks.org/" attribute="GFG>
</GFG:document>
Similar Reads
StAX XML Parser in Java
This article focuses on how one can parse a XML file in Java.XML : XML stands for eXtensible Markup Language. It was designed to store and transport data. It was designed to be both human- and machine-readable. Thatâs why, the design goals of XML emphasize simplicity, generality, and usability acros
6 min read
XML EventWriter in Java StAX
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-bas
5 min read
XML EventWriter in Java StAX API
Java StAX API was introduced in Java 6 and considered superior to DOM and SAX parsers. We can use Java StAX parser to read XML file and java Streaming API for XML (Java StAX) provides implementation for processing XML in java. The XMLEventWriter class in the Java StAX API allows you to write StAX XM
3 min read
The Factory Pattern in Scala
The factory method is used to offer a single interface to instantiate one of the multiple classes. In the Factory pattern, the objective is to make the object in such a way that it does not expose to the creation logic to the Client and can always refer to a newly created object with the help of a c
3 min read
XPath Evaluator in Java
As we know XML document is used to store and transport data. So, to access data from XML, we need something which could access each node and respective attributes data. Then the solution is XPath. XPath can be used to traverse through XML documents, to select nodes/elements, and attribute data. It i
3 min read
XMLStreamWriter in Java StAX
Streaming API for XML added in Java 6 provides a handy interface XMLStreamWriter which is used for writing XML documents. this API does not require building any specific object structure like in DOM and doing any intermediate tasks. It also supports namespaces by default which is very useful in more
3 min read
Abstract Factory Design Pattern in Java
The Abstract Factory Pattern in Java is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes, in simpler terms the Abstract Factory Pattern is a way of organizing how you create groups of things that ar
10 min read
DefaultHandler in SAX Parser in Java
SAX is nothing but a Simple API for XML and it is an event-based parser for XML documents. It differs from a DOM parser in such a way that a parse tree is not created by a SAX parser. The process is done in a sequential order starting at the top of the document and ending with the closing of the ROO
4 min read
What are factory functions in JavaScript ?
In JavaScript, a factory function is a function that returns an object. It is a way of creating and returning objects in a more controlled and customizable manner. Factory functions are a form of design pattern that enables the creation of objects with specific properties and behaviors. Why it is us
2 min read
StAX vs SAX Parser in Java
Streaming the API for XML, called the StAX, is an API for reading and writing the XML Documents. It was introduced in Java 6 and is considered superior to SAX and DOM which are other methods in Java to access XML. Java provides several ways [APIs] to access XML. Traditionally, XML APIs are either- T
3 min read