Java XML – JDOM2 – Outputter

Introduction

In the earlier tutorials we looked at how to create a JDOM2 document from SAX ,DOM and StAX. In this tutorial we learn how to output JDOM2 as XML, DOM, SAX, StAXEvent and StaxStream. Lets look at the important classes

Important Classes

  • XMLOutputter – This is probably the most useful outputter. It outputs the JDOM2 document as a stream of byes that can be written to a file. In other words, to create an XML file from JDOM2 document use this class. The most useful methods is public final void output(Document doc, OutputStream out). The encoding of the writer should match the encoding of the Format object of the Outputter. The conversion can be customized by using a custom XMLOutputProcessor
  • DOMOutputter – Outputs a JDOM2 document as a w3c DOM Document. It can be configured by passing in three types of objects – DOMAdapter, Format and DOMOutputProcessor. A DOMAdapter is an interface whose implementation interfaces with a DOMParser to create DOM objects. The default implementation is JAXPDOMAdapter. The DOMOutputProcessor is the main class responsible for the conversion. The important method is public final void output(Document doc, XMLStreamWriter out) throws XMLStreamException. An Element can also be passed instead of a Document
  • SAXOutputter – Outputs a JDOM2 document as a stream of SAX2 Events. It takes in a ContentHandler to process the SAX events. It can also be configured using an ErrorHandler, DTDHandler, EntityResolver, LexicalHandler and DeclHandler. It also uses a SAXOutputProcessor that does the bulk of the processing. The most useful constructor is public SAXOutputter(ContentHandler contentHandler) and the the output method is public final void output(Document doc, XMLStreamWriter out) throws XMLStreamException
  • StAXEventOutputter Outputs the JDOM2 document as a series of events that can be read by an XMLEventConsumer. The default processor to process the conversion is DefaultStAXEventProcessor. This can be customized, if required. The most useful method is public final void output(Document doc, XMLEventConsumer out) throws XMLStreamExceptionThe format can also be specified. The following formats are available :
    • for pretty printing – Format.getPrettyFormat()
    • for whitespace-normalized output – Format.getCompactFormat()
    • for unmodified-format output – Format.getRawFormat()
  • StAXStreamOutputter – Outputs the JDOM2 document as a stream that is handled by XMLStreamWriter. Here’s how the main method looks public final void output(Element element, XMLStreamWriter out) throws XMLStreamException. The default processor is DefaultStAXStreamProcessor
  • StAXStreamReader – Outputs the JDOM2 document as an XMLStreamReader.

Example

Now lets see the outputters in action

package com.studytrails.xml.jdom;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import javax.xml.stream.util.XMLEventConsumer;

import org.jdom2.Attribute;
import org.jdom2.Comment;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.output.DOMOutputter;
import org.jdom2.output.Format;
import org.jdom2.output.SAXOutputter;
import org.jdom2.output.StAXEventOutputter;
import org.jdom2.output.XMLOutputter;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class JDomOutputterExamples {

	public static void main(String[] args) throws JDOMException, XMLStreamException {

		// create the jdom
		Document jdomDoc = new Document();
		// create root element
		Element rootElement = new Element("Root");
		jdomDoc.setRootElement(rootElement);

		// add a comment
		Comment comment = new Comment("This is a comment");
		rootElement.addContent(comment);

		// add child
		Element child1 = new Element("child");
		child1.addContent("This is child 1");
		
		// add attribute
		Attribute attr1 = new Attribute("key1", "value1");
		child1.setAttribute(attr1);
		rootElement.addContent(child1);

		// Output as XML
		// create XMLOutputter
		XMLOutputter xml = new XMLOutputter();
		// we want to format the xml. This is used only for demonstration. pretty formatting adds extra spaces and is generally not required.
		xml.setFormat(Format.getPrettyFormat());
		System.out.println(xml.outputString(jdomDoc));
		

		// Output the JDOM2 document as a w3c Document
		// create the DOM Outputter
		DOMOutputter domOutputer = new DOMOutputter();
		// create the w3c Document from the JDOM2 Document
		org.w3c.dom.Document dom = domOutputer.output(jdomDoc);
		System.out.println(dom.getNodeName());

		//  we iterate through the w3c Document and print the elements
		org.w3c.dom.Element rootElementDom = dom.getDocumentElement();
		System.out.println(rootElementDom.getNodeName());
		NodeList children = rootElementDom.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			System.out.println("Node Name-->" + child.getNodeName());
			System.out.println("Node value-->" + child.getNodeValue());
			System.out.println("Node Attributes-->" + child.getAttributes());
		}

		// Output the JDOM2 as SAX events. Pass in the ContentHandler that will handle the events.
		SAXOutputter saxOutputer = new SAXOutputter(new myContentHandler());
		saxOutputer.output(jdomDoc);

		// Output as StaxEvents. Pass in a custom XMLEventConsumer.
		StAXEventOutputter staxOutputter = new StAXEventOutputter();
		staxOutputter.output(jdomDoc, new XMLEventConsumer() {

			@Override
			public void add(XMLEvent event) throws XMLStreamException {
				int eventType = event.getEventType();
				if (XMLEvent.COMMENT == eventType) {
					System.out.println(event.toString());
				}
				if (XMLEvent.START_ELEMENT == eventType) {
					System.out.println(event.asStartElement().getName());
				}

			}
		});
	}

	public static class myContentHandler extends DefaultHandler {
		@Override
		public void startDocument() throws SAXException {
			System.out.println("Start Doc");
			super.startDocument();
		}

		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
			System.out.println("Start " + localName);
			super.startElement(uri, localName, qName, attributes);
		}
	}

}

Leave a Reply

Your email address will not be published. Required fields are marked *