Java XML Node Serialize serializeElement(Element element, boolean omitXmlDeclaration, boolean indent, String method)

Here you can find the source of serializeElement(Element element, boolean omitXmlDeclaration, boolean indent, String method)

Description

serialize Element

License

Open Source License

Declaration

public static String serializeElement(Element element, boolean omitXmlDeclaration, boolean indent,
            String method) throws TransformerException 

Method Source Code


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

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 {
    public static String serializeElement(Element element, boolean omitXmlDeclaration, boolean indent,
            String method) throws TransformerException {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
        transformer.setOutputProperty(OutputKeys.METHOD, method);
        transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");

        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(element), new StreamResult(writer));
        return writer.getBuffer().toString();
    }/*from  w w  w. j  a  v a 2s .co  m*/
}

Related

  1. serialize(Node node, File outdir, String fileName)
  2. serialize(NodeList nodeList)
  3. serializeDom(Node domElement)
  4. serializeDom(Node node, StringBuffer writeString)
  5. serializeElement(Element element)
  6. serializeNode(Node node)
  7. serializeNode(Node node)
  8. serializeNode(Node node)
  9. serializeNode(Node node)