Java XML String Transform getString(Node node, boolean indent)

Here you can find the source of getString(Node node, boolean indent)

Description

Return the contents of the node as a string.

License

Open Source License

Parameter

Parameter Description
node a parameter
indent Indent the XML when formatting

Return

The node contents

Declaration

public static String getString(Node node, boolean indent) 

Method Source Code

//package com.java2s;
/*----------------------------------------------------------------------------- 
 * GDSC SMLM Software/*from   www  .  ja  v  a2 s.  co m*/
 * 
 * Copyright (C) 2013 Alex Herbert
 * Genome Damage and Stability Centre
 * University of Sussex, UK
 * 
 * This program 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.
 *---------------------------------------------------------------------------*/

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
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.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Node;

public class Main {
    /**
     * Return the contents of the node as a string. Any exceptions are ignored and the method returns null.
     * 
     * @param node
     * @param indent
     *            Indent the XML when formatting
     * @return The node contents
     */
    public static String getString(Node node, boolean indent) {
        Transformer transformer;
        try {
            transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, (indent) ? "yes" : "no");

            StreamResult result = new StreamResult(new StringWriter());
            DOMSource source = new DOMSource(node);
            transformer.transform(source, result);

            return result.getWriter().toString();
        } catch (TransformerConfigurationException e) {
            //e.printStackTrace();
        } catch (TransformerFactoryConfigurationError e) {
            //e.printStackTrace();
        } catch (TransformerException e) {
            //e.printStackTrace();
        }
        return "";
    }
}

Related

  1. getPropertyAsString(final Properties prop, final String comment)
  2. getSchema(String schemaString)
  3. getSchemaFromResource(String... files)
  4. getSourceAsString(Source source)
  5. getStreamResultForFile(String filename)
  6. getStringFromStreamSource(StreamSource src)
  7. getTemplates(String name)
  8. getValidator(String path)
  9. getXmlStringFromSource(Source payload)