Java XML Document Format prettyPrintXMLDocument(Node node)

Here you can find the source of prettyPrintXMLDocument(Node node)

Description

Create a pretty String representation of a DOM Document

License

Open Source License

Parameter

Parameter Description
node a parameter

Return

String of the given document

Declaration

public static String prettyPrintXMLDocument(Node node) 

Method Source Code

//package com.java2s;
/*/*from  www . java2  s. c om*/
 This file is part of p300.
    
    
 p300 is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 p300 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 General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with p300.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMImplementation;

import org.w3c.dom.Node;

import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

public class Main {
    /**
     * Create a pretty String representation of a DOM Document
     * @param node
     * @return String of the given document
     */
    public static String prettyPrintXMLDocument(Node node) {
        if (node == null) {
            return "";
        }
        DocumentBuilder builder = null;
        try {
            builder = getDocumentBuilder();
        } catch (ParserConfigurationException exception) {
            System.err.println(exception);
            return "";
        }
        DOMImplementation implementation = builder.getDOMImplementation();
        Object object = implementation.getFeature("LS", "3.0");
        if (!(object instanceof DOMImplementationLS)) {
            return "";
        }
        DOMImplementationLS loadSave = (DOMImplementationLS) object;
        LSSerializer serializer = loadSave.createLSSerializer();
        DOMConfiguration config = serializer.getDomConfig();
        if (config.canSetParameter("format-pretty-print", true)) {
            config.setParameter("format-pretty-print", true);
        }

        LSOutput loadSaveOut = loadSave.createLSOutput();
        StringWriter string = new StringWriter();
        loadSaveOut.setCharacterStream(string);
        loadSaveOut.setEncoding("UTF-8");
        serializer.write(node, loadSaveOut);
        String result = string.toString();

        //      String result = serializer.writeToString(node);
        return result;
    }

    /**
     * Return a fresh DocumentBuilder instance
     * @return
     * @throws ParserConfigurationException if there was a serious error
     */
    private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder;
    }
}

Related

  1. prettyPrint(Document doc)
  2. prettyPrint(Document doc, String programID, String xmlTraDestinationFolderPath)
  3. prettyPrint(final Document aNode)
  4. prettyPrintXml(Document doc)
  5. prettyPrintXml(Document document)
  6. print(Document d)
  7. printDoc(Document doc)
  8. printDoc(Document doc, PrintStream pstrm)
  9. printDoctype(Document doc, PrintStream pstrm)