Example usage for org.dom4j.io XMLWriter writeOpen

List of usage examples for org.dom4j.io XMLWriter writeOpen

Introduction

In this page you can find the example usage for org.dom4j.io XMLWriter writeOpen.

Prototype

public void writeOpen(Element element) throws IOException 

Source Link

Document

Writes the opening tag of an Element , including its Attribute s but without its content.

Usage

From source file:org.snipsnap.util.JDBCDatabaseExport.java

License:Open Source License

/**
 * Store snips and users from the SnipSpace to an xml document into a stream.
 * @param out outputstream to write to// w  w w  . j  a v a2 s .  com
 */
public static void store(OutputStream out, String appOid, Connection connection) {
    try {
        OutputFormat outputFormat = new OutputFormat();
        outputFormat.setEncoding("UTF-8");
        outputFormat.setNewlines(true);

        XMLWriter xmlWriter = new XMLWriter(out, outputFormat);
        xmlWriter.startDocument();
        Element root = DocumentHelper.createElement("snipspace");
        xmlWriter.writeOpen(root);

        //      storeUsers(xmlWriter, connection);
        storeSnips(xmlWriter, appOid, connection);

        xmlWriter.writeClose(root);
        xmlWriter.endDocument();
        xmlWriter.flush();
        xmlWriter.close();
    } catch (Exception e) {
        System.err.println("JDBCDatabaseExport: error while writing document: " + e.getMessage());
    }
}

From source file:org.waarp.openr66.database.data.DbTaskRunner.java

License:Open Source License

/**
 * Write the selected TaskRunners from PrepareStatement to a XMLWriter
 * //  w  w  w  .  j  a  v a2 s  . c om
 * @param preparedStatement
 *            ready to be executed
 * @param xmlWriter
 * @return the NbAndSpecialId for the number of transfer and higher rank found
 * @throws WaarpDatabaseNoConnectionException
 * @throws WaarpDatabaseSqlException
 * @throws OpenR66ProtocolBusinessException
 */
public static NbAndSpecialId writeXML(DbPreparedStatement preparedStatement, XMLWriter xmlWriter)
        throws WaarpDatabaseNoConnectionException, WaarpDatabaseSqlException, OpenR66ProtocolBusinessException {
    Element root = new DefaultElement(XMLRUNNERS);
    NbAndSpecialId nbAndSpecialId = new NbAndSpecialId();
    try {
        xmlWriter.writeOpen(root);
        Element node;
        while (preparedStatement.getNext()) {
            DbTaskRunner runner = DbTaskRunner.getFromStatement(preparedStatement);
            if (nbAndSpecialId.higherSpecialId < runner.specialId) {
                nbAndSpecialId.higherSpecialId = runner.specialId;
            }
            node = DbTaskRunner.getElementFromRunner(runner);
            xmlWriter.write(node);
            xmlWriter.flush();
            nbAndSpecialId.nb++;
        }
        xmlWriter.writeClose(root);
    } catch (IOException e) {
        logger.error("Cannot write XML file", e);
        throw new OpenR66ProtocolBusinessException("Cannot write file: " + e.getMessage());
    }
    return nbAndSpecialId;
}

From source file:org.waarp.openr66.database.data.DbTaskRunner.java

License:Open Source License

/**
 * Method to write the current DbTaskRunner for NoDb client instead of updating DB. 'setToArray'
 * must be called priorly to be able to store the values.
 * /*from   w  w w  .j  ava2s.  c om*/
 * @throws OpenR66ProtocolBusinessException
 */
public void writeXmlWorkNoDb() throws OpenR66ProtocolBusinessException {
    String filename = backendXmlFilename();
    OutputStream outputStream = null;
    XMLWriter xmlWriter = null;
    try {
        outputStream = new FileOutputStream(filename);
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("ISO-8859-1");
        xmlWriter = new XMLWriter(outputStream, format);
        Element root = new DefaultElement(XMLRUNNERS);
        try {
            xmlWriter.writeOpen(root);
            Element node;
            node = DbTaskRunner.getElementFromRunner(this);
            xmlWriter.write(node);
            xmlWriter.flush();
            xmlWriter.writeClose(root);
        } catch (IOException e) {
            logger.error("Cannot write XML file", e);
            throw new OpenR66ProtocolBusinessException("Cannot write file: " + e.getMessage());
        } catch (WaarpDatabaseSqlException e) {
            logger.error("Cannot write Data", e);
            throw new OpenR66ProtocolBusinessException("Cannot write Data: " + e.getMessage());
        }
    } catch (FileNotFoundException e) {
        logger.error("Cannot write XML file", e);
        throw new OpenR66ProtocolBusinessException("File not found");
    } catch (UnsupportedEncodingException e) {
        logger.error("Cannot write XML file", e);
        throw new OpenR66ProtocolBusinessException("Unsupported Encoding");
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.endDocument();
                xmlWriter.flush();
                xmlWriter.close();
            } catch (SAXException e) {
                try {
                    outputStream.close();
                } catch (IOException e2) {
                }
                File file = new File(filename);
                file.delete();
                logger.error("Cannot write XML file", e);
                throw new OpenR66ProtocolBusinessException("Unsupported Encoding");
            } catch (IOException e) {
                try {
                    outputStream.close();
                } catch (IOException e2) {
                }
                File file = new File(filename);
                file.delete();
                logger.error("Cannot write XML file", e);
                throw new OpenR66ProtocolBusinessException("Unsupported Encoding");
            }
        } else if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
            }
            File file = new File(filename);
            file.delete();
        }
    }
}