XML Output Factory in Java StAX
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
// Importing required classes 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; // Main class class GFG { // Main driver method public static void main(String[] args) { // Getting the XMLOutputFactory instance XMLOutputFactory factory= XMLOutputFactory.newInstance(); // Getting the XMLEventFactory instance XMLEventFactory eventFactory = XMLEventFactory.newInstance(); // Try block for error hadeling try { // Creating EventWriter object using factory(instance) XMLEventWriter writer = factory.createXMLEventWriter( new FileWriter( "D:\\gfg_OutputFactory.xml" )); // Creating XMLEvent object using eventFactory(instance) XMLEvent event = eventFactory.createStartDocument(); writer.add(event); // Creating a start element writer.add(event); // Creating namespace event = eventFactory.createNamespace( "GeeksforGeeks-practice" , "https://practice.geeksforgeeks.org/" ); writer.add(event); // Setting attributes event = eventFactory.createAttribute( "attribute" , "GFG" ); writer.add(event); // Lastly creating end element writer.add(event); // Flush and close xmlEventWriter writer.flush(); writer.close(); } // Catch block to handle exceptions catch (XMLStreamException e) { // Print line number where exception occurs // using printStacktrace() method 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>
Please Login to comment...