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.xalan.serialize.DOMSerializer;
14 import org.apache.xalan.serialize.SerializerToXML;
15 import org.w3c.dom.Document;
16 import org.xml.sax.SAXException;
20 * <p>This class constructs a pretty-printing XML Serializer
24 public class XMLHelper {
26 private XMLHelper() {}
28 public static DOMSerializer createDOMSerializer(Writer writer)
30 // XMLSerializer serializer = new XMLSerializer(writer, createOutputFormat());
31 // return serializer.asDOMSerializer();
32 SerializerToXML serializer = new SerializerToXML();
33 serializer.setWriter(writer);
34 return serializer.asDOMSerializer();
37 public static DOMSerializer createDOMSerializer(OutputStream stream)
39 // XMLSerializer serializer = new XMLSerializer(stream, createOutputFormat());
40 // return serializer.asDOMSerializer();
41 SerializerToXML serializer = new SerializerToXML();
42 serializer.setOutputStream(stream);
43 return serializer.asDOMSerializer();
45 // private static OutputFormat createOutputFormat() {
46 // OutputFormat format = new OutputFormat();
47 // format.setIndenting(true);
48 // format.setLineWidth(80);
52 public static Document createEmptyDocument() throws ParserConfigurationException {
53 DocumentBuilder builder = createDocumentBuilder();
54 return builder.newDocument();
57 public static Document createFromInputStream(InputStream stream)
58 throws IOException, SAXException, ParserConfigurationException {
59 DocumentBuilder builder = createDocumentBuilder();
60 return builder.parse(stream);
63 private static DocumentBuilder createDocumentBuilder()
64 throws FactoryConfigurationError, ParserConfigurationException {
65 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
66 return factory.newDocumentBuilder();
69 public static void write(Writer writer, Document document)
71 createDOMSerializer(writer).serialize(document);
74 public static void write(OutputStream stream, Document document)
76 createDOMSerializer(stream).serialize(document);