1 package com.quantum.util.xml;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
8 import javax.xml.parsers.DocumentBuilder;
9 import javax.xml.parsers.DocumentBuilderFactory;
10 import javax.xml.parsers.FactoryConfigurationError;
11 import javax.xml.parsers.ParserConfigurationException;
13 import org.apache.xml.serialize.DOMSerializer;
14 import org.apache.xml.serialize.OutputFormat;
15 import org.apache.xml.serialize.XMLSerializer;
16 import org.w3c.dom.Document;
17 import org.xml.sax.SAXException;
21 * <p>This class constructs a pretty-printing XML Serializer
25 public class XMLHelper {
27 private XMLHelper() {}
29 public static DOMSerializer createDOMSerializer(Writer writer)
31 XMLSerializer serializer = new XMLSerializer(writer, createOutputFormat());
32 return serializer.asDOMSerializer();
35 public static DOMSerializer createDOMSerializer(OutputStream stream)
37 XMLSerializer serializer = new XMLSerializer(stream, createOutputFormat());
38 return serializer.asDOMSerializer();
40 private static OutputFormat createOutputFormat() {
41 OutputFormat format = new OutputFormat();
42 format.setIndenting(true);
43 format.setLineWidth(80);
47 public static Document createEmptyDocument() throws ParserConfigurationException {
48 DocumentBuilder builder = createDocumentBuilder();
49 return builder.newDocument();
52 public static Document createFromInputStream(InputStream stream)
53 throws IOException, SAXException, ParserConfigurationException {
54 DocumentBuilder builder = createDocumentBuilder();
55 return builder.parse(stream);
58 private static DocumentBuilder createDocumentBuilder()
59 throws FactoryConfigurationError, ParserConfigurationException {
60 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
61 return factory.newDocumentBuilder();
64 public static void write(Writer writer, Document document)
66 createDOMSerializer(writer).serialize(document);
69 public static void write(OutputStream stream, Document document)
71 createDOMSerializer(stream).serialize(document);