Search in sources :

Example 11 with OutputFormat

use of org.apache.xml.serialize.OutputFormat in project vcell by virtualcell.

the class DOMUtil method serialize.

public static void serialize(Document document, OutputStream out) throws IOException {
    OutputFormat format = new OutputFormat();
    XMLSerializer serializer = new XMLSerializer(out, format);
    DOMSerializer domSerializer = serializer.asDOMSerializer();
    domSerializer.serialize(document);
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) DOMSerializer(org.apache.xml.serialize.DOMSerializer) OutputFormat(org.apache.xml.serialize.OutputFormat)

Example 12 with OutputFormat

use of org.apache.xml.serialize.OutputFormat in project mondrian by pentaho.

the class XmlUtility method save.

public static void save(Writer writer, Document document) throws IOException {
    OutputFormat outputFormat = new OutputFormat(document);
    outputFormat.setIndenting(true);
    outputFormat.setLineWidth(Integer.MAX_VALUE);
    outputFormat.setLineSeparator(Util.nl);
    try {
        XMLSerializer serializer = new XMLSerializer(writer, outputFormat);
        serializer.serialize(document);
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) OutputFormat(org.apache.xml.serialize.OutputFormat)

Example 13 with OutputFormat

use of org.apache.xml.serialize.OutputFormat in project Lucee by lucee.

the class StorageUtil method store.

/**
 * store loaded data to xml file
 * @param doc
 * @param res
 * @throws IOException
 */
public void store(Document doc, Resource res) throws IOException {
    OutputFormat format = new OutputFormat(doc, null, true);
    format.setLineSeparator("\r\n");
    format.setLineWidth(72);
    OutputStream os = null;
    try {
        XMLSerializer serializer = new XMLSerializer(os = res.getOutputStream(), format);
        serializer.serialize(doc.getDocumentElement());
    } finally {
        IOUtil.closeEL(os);
    }
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) OutputStream(java.io.OutputStream) OutputFormat(org.apache.xml.serialize.OutputFormat)

Example 14 with OutputFormat

use of org.apache.xml.serialize.OutputFormat in project Gemma by PavlidisLab.

the class AbstractGemmaEndpoint method writeReport.

/**
 * This method should/can only be used when the wrapper is manually built in the specific endpoints (ie. not using
 * the buildWrapper() in AbstractGemmaEndpoint).
 *
 * @param responseWrapper - Manually built wrapper
 * @param document        document
 * @param filename        - no xml extension is required
 */
protected void writeReport(Element responseWrapper, Document document, String filename) {
    String fullFileName = filename + ".xml";
    String path = HOME_DIR + File.separatorChar + "dataFiles" + File.separatorChar + "xml" + File.separatorChar;
    try {
        File file = new File(path, fullFileName);
        if (!file.exists()) {
            // in case of the subdirs doesn't exisit.
            new File(path).mkdirs();
            try (FileOutputStream out = new FileOutputStream(path + fullFileName)) {
                OutputFormat format = new OutputFormat(document);
                format.setIndenting(true);
                // to generate a file output use fileoutputstream
                /*
                     * "It is recommended that new applications use the DOM Level 3 LSSerializer or JAXP's
                     * Transformation API for XML (TrAX) for serializing XML"
                     */
                XMLSerializer serializer = new XMLSerializer(out, null);
                serializer.serialize(responseWrapper);
            }
            AbstractGemmaEndpoint.log.info("A report with the filename, " + fullFileName + ", has been created in path, " + path);
        } else
            AbstractGemmaEndpoint.log.info("A report with the filename, " + fullFileName + ", already exists.  A new report was not created.");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) OutputFormat(org.apache.xml.serialize.OutputFormat)

Example 15 with OutputFormat

use of org.apache.xml.serialize.OutputFormat in project mondrian by pentaho.

the class XmlUtil method toString.

/**
 * Convert a Node to a String.
 */
public static String toString(Node node, boolean prettyPrint) {
    if (node == null) {
        return null;
    }
    try {
        Document doc = node.getOwnerDocument();
        OutputFormat format;
        if (doc != null) {
            format = new OutputFormat(doc, null, prettyPrint);
        } else {
            format = new OutputFormat("xml", null, prettyPrint);
            // don't wrap lines
            format.setLineWidth(0);
        }
        if (prettyPrint) {
            format.setLineSeparator(LINE_SEP);
        } else {
            format.setLineSeparator("");
        }
        StringWriter writer = new StringWriter(1000);
        XMLSerializer serial = new XMLSerializer(writer, format);
        serial.asDOMSerializer();
        if (node instanceof Document) {
            serial.serialize((Document) node);
        } else if (node instanceof Element) {
            format.setOmitXMLDeclaration(true);
            serial.serialize((Element) node);
        } else if (node instanceof DocumentFragment) {
            format.setOmitXMLDeclaration(true);
            serial.serialize((DocumentFragment) node);
        } else if (node instanceof Text) {
            Text text = (Text) node;
            return text.getData();
        } else if (node instanceof Attr) {
            Attr attr = (Attr) node;
            String name = attr.getName();
            String value = attr.getValue();
            writer.write(name);
            writer.write("=\"");
            writer.write(value);
            writer.write("\"");
            if (prettyPrint) {
                writer.write(LINE_SEP);
            }
        } else {
            writer.write("node class = " + node.getClass().getName());
            if (prettyPrint) {
                writer.write(LINE_SEP);
            } else {
                writer.write(' ');
            }
            writer.write("XmlUtil.toString: fix me: ");
            writer.write(node.toString());
            if (prettyPrint) {
                writer.write(LINE_SEP);
            }
        }
        return writer.toString();
    } catch (Exception ex) {
        // ignore
        return null;
    }
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) OutputFormat(org.apache.xml.serialize.OutputFormat)

Aggregations

OutputFormat (org.apache.xml.serialize.OutputFormat)19 XMLSerializer (org.apache.xml.serialize.XMLSerializer)19 FileOutputStream (java.io.FileOutputStream)5 Document (org.w3c.dom.Document)5 File (java.io.File)4 IOException (java.io.IOException)4 OutputStream (java.io.OutputStream)3 StringWriter (java.io.StringWriter)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DOMSerializer (org.apache.xml.serialize.DOMSerializer)2 Element (org.w3c.dom.Element)2 SAXException (org.xml.sax.SAXException)2 SkipColumns (com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns)1 SkipContent (com.axway.ats.action.dbaccess.snapshot.rules.SkipContent)1 SkipIndexAttributes (com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes)1 SkipRows (com.axway.ats.action.dbaccess.snapshot.rules.SkipRows)1 DatabaseSnapshotException (com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)1 TableDescription (com.axway.ats.common.dbaccess.snapshot.TableDescription)1 FileSystemSnapshotException (com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException)1 AtsConfigurationException (com.axway.ats.core.atsconfig.exceptions.AtsConfigurationException)1