Example usage for org.w3c.dom.ls LSSerializer writeToString

List of usage examples for org.w3c.dom.ls LSSerializer writeToString

Introduction

In this page you can find the example usage for org.w3c.dom.ls LSSerializer writeToString.

Prototype

public String writeToString(Node nodeArg) throws DOMException, LSException;

Source Link

Document

Serialize the specified node as described above in the general description of the LSSerializer interface.

Usage

From source file:Main.java

/**
 * Serializes a DOM Document to XML// w w w.  ja  v a2  s.  c o  m
 * 
 * @param document
 *            a DOM document
 * @return the Document serialized to XML
 */
public static String toXML(Document document) {
    DOMImplementationLS domLS = (DOMImplementationLS) implementation;
    LSSerializer serializer = domLS.createLSSerializer();
    String s = serializer.writeToString(document);

    return s;
}

From source file:javatojs.DomUtil.java

public static void writeDocument(Document document) {
    try {//from   www. j a  va2  s.  co m
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        String documentStr = writer.writeToString(document);
        System.out.println("Serialized document: \n" + documentStr);
    } catch (Exception e) {
        System.out.println("ERROR writing document: \n ");
        e.printStackTrace();
    }
}

From source file:Main.java

public static String toNormalizedXML(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//from  w w  w .j a v a2 s .c om
    dbf.setCoalescing(true);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(is);
    document.normalizeDocument();
    document.getDocumentElement().setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", //$NON-NLS-1$
            "xsi:schemaLocation", //$NON-NLS-1$
            "http://abc4trust.eu/wp2/abcschemav1.0 ../../../../../../../../../abc4trust-xml/src/main/resources/xsd/schema.xsd"); //$NON-NLS-1$
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    String xml = serializer.writeToString(document);

    return trim(xml);
}

From source file:Main.java

public static String convertToString(Node node) {
    boolean withXMLDeclaration = true;
    String result;//  www . j a  va2 s . c  o  m
    if (withXMLDeclaration) {
        Document document = node.getOwnerDocument();
        DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
        LSSerializer serializer = domImplLS.createLSSerializer();
        result = serializer.writeToString(node);
    } else {
        try {
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            StringWriter buffer = new StringWriter();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(new DOMSource(node), new StreamResult(buffer));
            result = buffer.toString();
        } catch (TransformerConfigurationException e) {
            result = "";
        } catch (TransformerException e) {
            result = "";
        }
    }
    return result;
}

From source file:Main.java

public static String serializeNode(Node node) throws LSException, IllegalAccessException, DOMException,
        InstantiationException, ClassNotFoundException, ClassCastException {
    String serializedElement = null;
    if (node != null) {
        System.setProperty(DOMImplementationRegistry.PROPERTY,
                "org.apache.xerces.dom.DOMImplementationSourceImpl");
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        serializedElement = writer.writeToString(node);
    }//from w ww  . j a va 2 s  . c om
    return serializedElement;
}

From source file:Main.java

public static String serializeElement(Element element) throws LSException, IllegalAccessException, DOMException,
        InstantiationException, ClassNotFoundException, ClassCastException {
    String serializedElement = null;
    if (element != null) {
        System.setProperty(DOMImplementationRegistry.PROPERTY,
                "org.apache.xerces.dom.DOMImplementationSourceImpl");
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        serializedElement = writer.writeToString(element);
    }//from   w w  w  .  j  a  v  a  2  s. com
    return serializedElement;
}

From source file:Main.java

/**
 * Serializes the given document into a string
 * /*from  w ww.  ja  v  a  2s .  c o  m*/
 * @param doc The document to serialize
 * @return The XML document as a string (pretty-printed)
 */
public static String documentToString(Document doc) {
    if (doc == null)
        return "null";
    LSSerializer writer = impl.createLSSerializer();
    writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
    return writer.writeToString(doc);
}

From source file:Main.java

/**
 * Node to string./* ww w  . j a  va2s.c o  m*/
 *
 * @param node
 *            the node
 * @return the string
 */
public static String nodeToString(Node node) {
    Document doc = node.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) doc.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    serializer.getDomConfig().setParameter("xml-declaration", false);
    String string = serializer.writeToString(node);
    return string.trim();
}

From source file:Main.java

public static String XMLtoString(Node node) throws Exception {
    Document document = node.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    serializer.getDomConfig().setParameter("xml-declaration", false);
    return serializer.writeToString(node);
}

From source file:eu.dasish.annotation.backend.Helpers.java

public static String elementToString(Element element) {
    Document document = element.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    String result = serializer.writeToString(element);
    return result;
}