Java XML Transform Usage safeToXml(Element element)

Here you can find the source of safeToXml(Element element)

Description

safe To Xml

License

Open Source License

Declaration

public static String safeToXml(Element element) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.StringWriter;

import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    public static String safeToXml(Element element) {
        try {//from  w ww .  j  av  a2 s  . co  m
            return toXml(element);
        } catch (TransformerException e) {
            return "Error transforming to xml: " + e.toString();
        }
    }

    public static String toXml(Node node) throws TransformerException {
        Source src = new DOMSource(node);
        return toXml(src, -1);
    }

    public static String toXml(XMLStreamReader xml) throws TransformerException {
        Source src = new StAXSource(xml);
        return toXml(src, -1);
    }

    public static String toXml(Source src, int indent)
            throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
        Transformer transformer = buildXmlTransformer();
        if (indent >= 0) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
        }

        StringWriter writer = new StringWriter();

        Result dest = new StreamResult(writer);
        transformer.transform(src, dest);

        return writer.getBuffer().toString();
    }

    public static String toXml(Document xmlDocument) throws TransformerException {
        return toXml(xmlDocument.getDocumentElement());
    }

    private static Transformer buildXmlTransformer()
            throws TransformerFactoryConfigurationError, TransformerConfigurationException {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        return transformer;
    }
}

Related

  1. parse(File f)
  2. parse(final InputStream file, final ContentHandler handler)
  3. path(Element element)
  4. readDoc(Reader in)
  5. renderElement(Element theElem)
  6. streamSource(File file)
  7. toNonValidatingSAXSource(InputStream in)
  8. toXMLInputSource(StreamSource in)
  9. unwrapException(Throwable t)