Java XML Document Format formatXML(Document xml, Document xsl, URIResolver resolver, Writer output)

Here you can find the source of formatXML(Document xml, Document xsl, URIResolver resolver, Writer output)

Description

format XML

License

Open Source License

Declaration

public static void formatXML(Document xml, Document xsl, URIResolver resolver, Writer output) throws Exception 

Method Source Code

//package com.java2s;
/*//  www.  j a va 2  s  .c om
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.Reader;

import java.io.Writer;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.URIResolver;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

import org.xml.sax.InputSource;

public class Main {
    public static void formatXML(Document xml, Document xsl, URIResolver resolver, Writer output) throws Exception {
        try {
            DOMSource xslSource = new DOMSource(xsl);
            DOMSource xmlSource = new DOMSource(xml);
            Result result = new StreamResult(output);
            formatXML(xmlSource, xslSource, resolver, result);
        } finally {
            output.close();
        }
    }

    public static void formatXML(Document xml, Document xsl, Writer output) throws Exception {
        formatXML(xml, xsl, null, output);
    }

    public static void formatXML(Reader xml, Reader xsl, URIResolver resolver, Writer output) throws Exception {
        try {
            try {
                try {
                    Source xmlSource = new SAXSource(new InputSource(xml));
                    Source xslSource = new SAXSource(new InputSource(xsl));
                    Result result = new StreamResult(output);
                    formatXML(xmlSource, xslSource, resolver, result);
                } finally {
                    output.close();
                }
            } finally {
                xsl.close();
            }
        } finally {
            xml.close();
        }
    }

    public static void formatXML(Reader xml, Reader xsl, Writer output) throws Exception {
        formatXML(xml, xsl, null, output);
    }

    /**
     * @param xmlSource
     * @param xslSource
     * @param resolver
     * @param output
     * @throws TransformerFactoryConfigurationError
     * @throws TransformerConfigurationException
     * @throws TransformerException
     */
    public static void formatXML(Source xmlSource, Source xslSource, URIResolver resolver, Result result)
            throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();
        Templates t = factory.newTemplates(xslSource);
        Transformer transformer = t.newTransformer();
        if (resolver != null) {
            transformer.setURIResolver(resolver);
        }
        transformer.transform(xmlSource, result);
    }
}

Related

  1. formatElementStart( Document document, Element element, String formatString)
  2. formatNode(Document doc, Node parent, Node node)
  3. formatPython(int start, Document doc, String s)
  4. formatXML(Document document, org.w3c.dom.Element root, String tab)
  5. formatXML(Document xml, Document xsl)
  6. pretty(Document document, OutputStream outputStream, int indent)
  7. prettyFormat(Document doc)
  8. prettyPrint(Document doc)
  9. prettyPrint(Document doc, String programID, String xmlTraDestinationFolderPath)