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);
event = eventFactory.createEndElement( "GFG" , "http://gfg.com" , "document" );
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>
Share your thoughts in the comments
Please Login to comment...