Java XML Transform Usage renderElement(Element theElem)

Here you can find the source of renderElement(Element theElem)

Description

render Element

License

Open Source License

Declaration

public static String renderElement(Element theElem) 

Method Source Code


//package com.java2s;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMSource;
import java.io.Writer;
import java.io.StringWriter;
import org.w3c.dom.Element;

public class Main {
    public static String renderElement(Element theElem) {
        try {//  ww w .  j  ava 2s . co m
            Writer aOutput = new StringWriter();

            Source aSource = new DOMSource(theElem);
            StreamResult aStreamResult = new StreamResult(aOutput);

            Transformer aTransformer = TransformerFactory.newInstance().newTransformer();

            aTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
            aTransformer.setOutputProperty(OutputKeys.METHOD, "html");
            aTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            aTransformer.setOutputProperty(OutputKeys.STANDALONE, "no");

            aTransformer.transform(aSource, aStreamResult);

            aOutput.close();

            // hack because the   elements were getting escaped for some reason
            return aOutput.toString().replaceAll(" ", " ");
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}

Related

  1. paramsEqual(AlgorithmParameterSpec spec1, AlgorithmParameterSpec spec2)
  2. parse(File f)
  3. parse(final InputStream file, final ContentHandler handler)
  4. path(Element element)
  5. readDoc(Reader in)
  6. safeToXml(Element element)
  7. streamSource(File file)
  8. toNonValidatingSAXSource(InputStream in)
  9. toXMLInputSource(StreamSource in)