Example usage for org.w3c.dom Document getImplementation

List of usage examples for org.w3c.dom Document getImplementation

Introduction

In this page you can find the example usage for org.w3c.dom Document getImplementation.

Prototype

public DOMImplementation getImplementation();

Source Link

Document

The DOMImplementation object that handles this document.

Usage

From source file:Main.java

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

    // Client assumes/requires UTF-8 response.
    LSOutput lsOutput = domImplLS.createLSOutput();
    lsOutput.setEncoding("UTF-8");

    StringWriter stringWriter = new StringWriter();
    lsOutput.setCharacterStream(stringWriter);
    serializer.write(element, lsOutput);

    return stringWriter.toString();
}

From source file:Main.java

static public String getInnerXmlText(Node xmlNode) {
    StringBuilder result = new StringBuilder();

    Document xmlDocument = xmlNode.getOwnerDocument();
    DOMImplementation xmlDocumentImpl = xmlDocument.getImplementation();
    DOMImplementationLS lsImpl = (DOMImplementationLS) xmlDocumentImpl.getFeature("LS", "3.0");
    LSSerializer lsSerializer = lsImpl.createLSSerializer();

    NodeList childNodes = xmlNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        String childText = lsSerializer.writeToString(childNodes.item(i));
        int pos = childText.indexOf("?>");
        if (pos > -1) {
            childText = childText.substring(pos + 2);
        }//from ww w .j av a 2s .  c  o  m
        result.append(childText);
    }

    return result.toString();
}

From source file:Main.java

public static void writeDocumentToDisk(Document document, File directoryToWriteTo, String fileName) {
    DOMImplementation domImplementation = document.getImplementation();
    if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS",
                "3.0");
        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
        DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
        if (domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) {
            lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
            LSOutput lsOutput = domImplementationLS.createLSOutput();
            lsOutput.setEncoding("UTF-8");
            try {
                lsOutput.setByteStream(new FileOutputStream(new File(directoryToWriteTo, fileName)));
            } catch (FileNotFoundException e) {
                throw new IllegalStateException(e);
            }//  ww  w .  ja  v  a 2 s  .  c  om
            lsSerializer.write(document, lsOutput);
        }
    }

}

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:Main.java

public static String convertToString(Node node) {
    boolean withXMLDeclaration = true;
    String result;/*from ww w  .j  av a  2 s.com*/
    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

/**
 * Converts a Document to a String//  ww w  .  jav a2  s  .  c  om
 * 
 * @param doc
 *            The Document to be converted
 * @return The String representation of the Document
 */
public static String convertDocumentToString(final Document doc) {
    final DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
    final LSSerializer lsSerializer = domImplementation.createLSSerializer();
    // lsSerializer.getDomConfig().setParameter("format-pretty-print",
    // Boolean.TRUE);
    final String xml = lsSerializer.writeToString(doc);

    return xml;
}

From source file:Main.java

/**
 * Serialize XML Node to string/*w  w  w .j  a va  2  s  .  com*/
 * <p>
 * Note: this method is supposed to be faster than the Transform version but the output control
 * is limited. If node is Document node, it will output XML PI which sometimes we want to avoid.
 * 
 * @param doc XML document
 * @param node Node to be serialized
 * @param encoding encoding for the output
 * @return String representation of the Document
 * @throws IOException
 */
public static String serializeToStringLS(Document doc, Node node, String encoding) throws IOException {
    DOMImplementationLS domImpl = (DOMImplementationLS) doc.getImplementation();
    LSSerializer lsSerializer = domImpl.createLSSerializer();
    LSOutput output = domImpl.createLSOutput();
    output.setEncoding(encoding);
    StringWriter writer = new StringWriter();
    output.setCharacterStream(writer);
    lsSerializer.write(node, output);
    writer.flush();

    return writer.toString();
}

From source file:com.qualogy.qafe.bind.orm.jibx.ORMBinder.java

public static String docToString(Document doc) {
    DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
    LSSerializer lsSerializer = domImplementation.createLSSerializer();
    return lsSerializer.writeToString(doc);
}

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;
}

From source file:Main.java

public static boolean serialize(Document doc, boolean setXmlDecl, File f) throws FileNotFoundException {
    DOMImplementationLS lsImpl = (DOMImplementationLS) doc.getImplementation().getFeature("LS", "3.0");
    LSSerializer serializer = lsImpl.createLSSerializer();
    serializer.getDomConfig().setParameter("xml-declaration", setXmlDecl); // set it to false to get

    LSOutput output = lsImpl.createLSOutput();
    output.setByteStream(new FileOutputStream(f));
    return serializer.write(doc, output);
}