Example usage for org.w3c.dom.ls DOMImplementationLS createLSOutput

List of usage examples for org.w3c.dom.ls DOMImplementationLS createLSOutput

Introduction

In this page you can find the example usage for org.w3c.dom.ls DOMImplementationLS createLSOutput.

Prototype

public LSOutput createLSOutput();

Source Link

Document

Create a new empty output destination object where LSOutput.characterStream, LSOutput.byteStream, LSOutput.systemId, LSOutput.encoding are null.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from   w  w w. j  av a 2 s.com*/
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();

    Element root = doc.createElementNS(null, "person"); // Create Root Element
    Element item = doc.createElementNS(null, "name"); // Create element
    item.appendChild(doc.createTextNode("Jeff"));
    root.appendChild(item); // Attach element to Root element
    item = doc.createElementNS(null, "age"); // Create another Element
    item.appendChild(doc.createTextNode("28"));
    root.appendChild(item); // Attach Element to previous element down tree
    item = doc.createElementNS(null, "height");
    item.appendChild(doc.createTextNode("1.80"));
    root.appendChild(item); // Attach another Element - grandaugther
    doc.appendChild(root); // Add Root to Document

    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS");

    LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer
                                                       // for the DOM
    LSOutput out = domImplLS.createLSOutput();
    StringWriter stringOut = new StringWriter(); // Writer will be a String
    out.setCharacterStream(stringOut);
    ser.write(doc, out); // Serialize the DOM

    System.out.println("STRXML = " + stringOut.toString()); // DOM as a String
}

From source file:DOMGenerate.java

public static void main(String[] argv) {
    try {/*  w w  w  . ja v a  2 s. c o m*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();

        Element root = doc.createElementNS(null, "person"); // Create Root Element
        Element item = doc.createElementNS(null, "name"); // Create element
        item.appendChild(doc.createTextNode("Jeff"));
        root.appendChild(item); // Attach element to Root element
        item = doc.createElementNS(null, "age"); // Create another Element
        item.appendChild(doc.createTextNode("28"));
        root.appendChild(item); // Attach Element to previous element down tree
        item = doc.createElementNS(null, "height");
        item.appendChild(doc.createTextNode("1.80"));
        root.appendChild(item); // Attach another Element - grandaugther
        doc.appendChild(root); // Add Root to Document

        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS");

        LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer
                                                           // for the DOM
        LSOutput out = domImplLS.createLSOutput();
        StringWriter stringOut = new StringWriter(); // Writer will be a String
        out.setCharacterStream(stringOut);
        ser.write(doc, out); // Serialize the DOM

        System.out.println("STRXML = " + stringOut.toString()); // Spit out the
                                                                // DOM as a String
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0 LS 3.0");
    if (impl == null) {
        System.out.println("No DOMImplementation found !");
        System.exit(0);/*from  www  .jav a2s. c  o m*/
    }

    System.out.printf("DOMImplementationLS: %s\n", impl.getClass().getName());

    LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/TR/REC-xml");
    // http://www.w3.org/2001/XMLSchema
    System.out.printf("LSParser: %s\n", parser.getClass().getName());

    Document doc = parser.parseURI("");

    LSSerializer serializer = impl.createLSSerializer();
    LSOutput output = impl.createLSOutput();
    output.setEncoding("UTF-8");
    output.setByteStream(System.out);
    serializer.write(doc, output);
    System.out.println();
}

From source file:DOM3.java

public static void main(String[] argv) {

    if (argv.length == 0) {
        printUsage();//from w  ww  .  ja v  a 2s  . co m
        System.exit(1);
    }

    try {

        // get DOM Implementation using DOM Registry
        System.setProperty(DOMImplementationRegistry.PROPERTY,
                "org.apache.xerces.dom.DOMXSImplementationSourceImpl");
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");

        // create DOMBuilder
        builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

        DOMConfiguration config = builder.getDomConfig();

        // create Error Handler
        DOMErrorHandler errorHandler = new DOM3();

        // create filter
        LSParserFilter filter = new DOM3();

        builder.setFilter(filter);

        // set error handler
        config.setParameter("error-handler", errorHandler);

        // set validation feature
        // config.setParameter("validate", Boolean.FALSE);
        config.setParameter("validate", Boolean.TRUE);

        // set schema language
        config.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema");
        // config.setParameter("psvi",Boolean.TRUE);
        // config.setParameter("schema-type","http://www.w3.org/TR/REC-xml");

        // set schema location
        config.setParameter("schema-location", "personal.xsd");

        // parse document
        System.out.println("Parsing " + argv[0] + "...");
        Document doc = builder.parseURI(argv[0]);

        // set error handler on the Document
        config = doc.getDomConfig();

        config.setParameter("error-handler", errorHandler);

        // set validation feature
        config.setParameter("validate", Boolean.TRUE);
        config.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema");
        // config.setParameter("schema-type","http://www.w3.org/TR/REC-xml");
        config.setParameter("schema-location", "data/personal.xsd");

        // remove comments from the document
        config.setParameter("comments", Boolean.FALSE);

        System.out.println("Normalizing document... ");
        doc.normalizeDocument();

        // create DOMWriter
        LSSerializer domWriter = impl.createLSSerializer();

        System.out.println("Serializing document... ");
        config = domWriter.getDomConfig();
        config.setParameter("xml-declaration", Boolean.FALSE);
        // config.setParameter("validate",errorHandler);

        // serialize document to standard output
        // domWriter.writeNode(System.out, doc);
        LSOutput dOut = impl.createLSOutput();
        dOut.setByteStream(System.out);
        domWriter.write(doc, dOut);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static <T extends OutputStream> T lsSerializeDom(Node doc, T byteStream, String encoding)
        throws Exception {
    if (doc == null)
        return byteStream;
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");

    LSOutput lsOutput = impl.createLSOutput();
    lsOutput.setByteStream(byteStream);//from w ww .  j  a v  a2s.co m
    encoding = encoding == null ? "UTF-8" : encoding;
    lsOutput.setEncoding(encoding);

    impl.createLSSerializer().write(doc, lsOutput);

    return byteStream;
}

From source file:Main.java

public static final String prettyPrint(final Document aNode) throws Exception {
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

    DOMImplementationLS impls = (DOMImplementationLS) registry.getDOMImplementation("LS");

    // Prepare the output
    LSOutput domOutput = impls.createLSOutput();
    domOutput.setEncoding(java.nio.charset.Charset.defaultCharset().name());
    StringWriter writer = new StringWriter();
    domOutput.setCharacterStream(writer);
    LSSerializer domWriter = impls.createLSSerializer();
    DOMConfiguration domConfig = domWriter.getDomConfig();
    domConfig.setParameter("format-pretty-print", true);
    domConfig.setParameter("element-content-whitespace", true);
    domWriter.setNewLine("\r\n");
    domConfig.setParameter("cdata-sections", Boolean.TRUE);
    // And finaly, write
    domWriter.write(aNode, domOutput);//from w w w  .jav a 2  s  .  c o m
    return domOutput.getCharacterStream().toString();
}

From source file:Main.java

public static boolean writeDocumentToStream(Document doc, OutputStream os) {
    // Check if DOM Load and Save is supported

    if (!((doc.getFeature("Core", "3.0") != null) && (doc.getFeature("LS", "3.0") != null)))
        throw new RuntimeException("DOM Load and Save unsupported");

    // Grab the available implementation

    DOMImplementationLS DOMiLS = (DOMImplementationLS) (doc.getImplementation()).getFeature("LS", "3.0");

    // Create LS output destination

    LSOutput lso = DOMiLS.createLSOutput();
    lso.setByteStream(os);/*from ww  w. jav  a2  s .c  om*/

    // Create a LS serializer
    // and tell it to make the output 'pretty'

    LSSerializer serializer = DOMiLS.createLSSerializer();
    serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);

    // Serialize the xml to your output stream

    return serializer.write(doc, lso);
}

From source file:Main.java

public static void writeNode(Node node, OutputStream os, boolean formatted)
        throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry
            .getDOMImplementation("LS");

    LSOutput lsOutput = domImplementationLS.createLSOutput();
    lsOutput.setEncoding("UTF-8");
    lsOutput.setByteStream(os);/*  ww  w. j  a  va 2s.com*/

    LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
    if (formatted)
        setLsSeriliserToFormatted(lsSerializer);
    lsSerializer.write(node, lsOutput);
}

From source file:Main.java

/**
 * Serialize XML Node to string//from w  w w .j  a  v  a2s  . c o m
 * <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: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();
}