Java XML DOM Print printTreeDOM(final Element samlToken, final boolean isIndent)

Here you can find the source of printTreeDOM(final Element samlToken, final boolean isIndent)

Description

Prints the tree DOM.

License

EUPL

Parameter

Parameter Description
samlToken the SAML token
isIndent the is indent

Exception

Parameter Description
TransformerException the exception

Return

the string

Declaration

public static String printTreeDOM(final Element samlToken, final boolean isIndent) throws TransformerException 

Method Source Code

//package com.java2s;
/*//from w  w w  .  j a va 2s  .  co m
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence. You may
 * obtain a copy of the Licence at:
 *
 * http://www.osor.eu/eupl/european-union-public-licence-eupl-v.1.1
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * Licence for the specific language governing permissions and limitations under
 * the Licence.
 */

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Element;

public class Main {
    /**
     * Prints the tree DOM.
     *
     * @param samlToken the SAML token
     * @param isIndent the is indent
     *
     * @return the string
     * @throws TransformerException the exception
     */
    public static String printTreeDOM(final Element samlToken, final boolean isIndent) throws TransformerException {
        // set up a transformer
        final TransformerFactory transfac = TransformerFactory.newInstance();
        final Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, String.valueOf(isIndent));

        // create string from XML tree
        final StringWriter stringWriter = new StringWriter();
        final StreamResult result = new StreamResult(stringWriter);
        final DOMSource source = new DOMSource(samlToken);
        trans.transform(source, result);
        final String xmlString = stringWriter.toString();

        return xmlString;
    }
}

Related

  1. prettyPrintDOMAsHTML(Node node, OutputStream stream)
  2. printDom(Node dom, OutputStream os)
  3. printDOM(Node node, OutputStream out, String encoding)
  4. printDOM(Node root, OutputStream os)
  5. printDOMTree(Node node, PrintWriter out, String docType, String copyright)