Example usage for org.dom4j.io XMLWriter write

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

Introduction

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

Prototype

public void write(Object object) throws IOException 

Source Link

Document

Writes the given object which should be a String, a Node or a List of Nodes.

Usage

From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void backup1table(String strPath, String strTableName) {
    Connection con = null;/* w  w  w. j  a  v  a  2s.co  m*/

    XMLWriter output = null;

    ResultSet rsTable = null;

    Statement stTable = null;

    try {
        if (!(strPath.substring(strPath.length() - 1).equals(File.separator)
                || strPath.substring(strPath.length() - 1).equals("/"))) {
            strPath = strPath + File.separatorChar;
        }

        //         Class.forName("org.postgresql.Driver").newInstance();
        //
        //         String strUrl = "jdbc:postgresql://localhost:5432/hm";
        //
        //         String strUsr = "hivemanager";
        //
        //         String strPsd = "aerohive";

        //         con = DriverManager
        //               .getConnection(strUrl, strUsr, strPsd);
        AhBackupNewTool oTool = new AhBackupNewTool();

        con = oTool.initCon();

        //String strTableName = AhBackupTool.ahLicenseHistory.toLowerCase();

        try {
            stTable = con.createStatement();

            //            String strSql = "select count(*) from " + strTableName;
            //
            //            rsTable = stTable.executeQuery(strSql);
            //
            //            rsTable = stTable.executeQuery(strSql);
            //
            //            rsTable.next();
            //
            //            int iRowCount = rsTable.getInt(1);
            //
            //            rsTable.close();

            //int intFileCount = 1;
            String strSql;
            int intFileCount = 0;
            int intRecordNum;

            //for (int i = 0; i < iRowCount || i == 0; i = i + 5000) {
            while (true) {
                intRecordNum = 0;

                strSql = "select * from " + strTableName + " limit " + 5000 + " offset " + intFileCount * 5000;
                if (!AhBackupNewTool.isValidCoon(con)) {
                    con = oTool.initCon();
                    stTable = con.createStatement();
                }

                rsTable = stTable.executeQuery(strSql);

                Document document = DocumentHelper.createDocument();

                Element table = document.addElement("table").addAttribute("schema", strTableName);

                ResultSetMetaData rsmd = rsTable.getMetaData();

                int iCount = rsmd.getColumnCount();

                while (rsTable.next()) {
                    intRecordNum++;
                    Element row = table.addElement("row");

                    for (int icol = 1; icol <= iCount; icol++) {
                        String newStr;

                        if (rsTable.getString(icol) == null) {
                            newStr = "NULL";
                        } else if ("null".equalsIgnoreCase(rsTable.getString(icol))) {
                            newStr = "_" + rsTable.getString(icol) + "_";
                        } else {
                            newStr = rsTable.getString(icol);
                        }

                        if (1 == intRecordNum) {
                            row.addElement("field").addAttribute("name", rsmd.getColumnName(icol))
                                    .addAttribute("value", newStr);
                        } else {
                            row.addElement("field").addAttribute("value", newStr);
                        }
                    }
                }

                if (intRecordNum <= 0 && 0 != intFileCount)
                    break;

                File file;

                if (intFileCount == 0) {
                    file = new File(strPath + strTableName.toLowerCase() + ".xml");

                } else {
                    file = new File(strPath + strTableName.toLowerCase() + "_" + intFileCount + ".xml");
                }

                intFileCount++;

                output = new XMLWriter(new FileOutputStream(file));

                output.write(document);

                output.close();

                if (5000 > intRecordNum)
                    break;
            }

            rsTable.close();

            stTable.close();
        } catch (Exception ex) {
            BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
        }
    } catch (Exception ex) {
        // add the debug msg
        BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
    } finally {
        if (null != con) {
            try {
                con.close();
            } catch (Exception conex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, conex.getMessage());
            }
        }

        if (null != output) {
            try {
                output.close();
            } catch (Exception outex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, outex.getMessage());
            }
        }

        if (null != rsTable) {
            try {
                rsTable.close();
            } catch (Exception rsex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, rsex.getMessage());
            }
        }

        if (null != stTable) {
            try {
                stTable.close();
            } catch (Exception stex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, stex.getMessage());
            }
        }
    }
}

From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void storeBriefDomainInfo(String strXmlPath, HmDomain oDomain) {
    String strDomainTable = AhBackupTool.ahDomain.toLowerCase();

    Document document = DocumentHelper.createDocument();

    Element table = document.addElement("table").addAttribute("schema", strDomainTable);

    File fDomain = new File(strXmlPath + strDomainTable.toLowerCase() + ".xml");

    XMLWriter output = null;

    try {//from   w ww .ja v a 2  s. co m
        output = new XMLWriter(new FileOutputStream(fDomain));

        Element row = table.addElement("row");

        String strDomainId = oDomain.getId().toString();

        String strDomainName = oDomain.getDomainName();

        row.addElement("field").addAttribute("name", "id").addAttribute("value", strDomainId);

        row.addElement("field").addAttribute("name", "domainname").addAttribute("value", strDomainName);

        output.write(document);

        output.close();
    } catch (Exception ex) {
        BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
    } finally {

        if (null != output) {
            try {
                output.close();
            } catch (Exception outex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, outex.getMessage());
            }
        }
    }
}

From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void storeDomainTable(String strTableName, String strPath, HmDomain oDomain, Connection conTable) {
    String strSql = "select * from " + strTableName;

    XMLWriter output = null;

    ResultSet rsTable = null;/*  w ww.  j av  a 2 s  .  co  m*/

    Statement stTable = null;

    try {
        stTable = conTable.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

        rsTable = stTable.executeQuery(strSql);

        Document document = DocumentHelper.createDocument();

        Element table = document.addElement("table").addAttribute("schema", strTableName);

        File file = new File(strPath + strTableName.toLowerCase() + ".xml");

        output = new XMLWriter(new FileOutputStream(file));

        ResultSetMetaData rsmd = rsTable.getMetaData();

        int iCount = rsmd.getColumnCount();

        while (rsTable.next()) {
            Element row = table.addElement("row");

            for (int icol = 1; icol <= iCount; icol++) {
                String newStr;

                if (rsTable.getString(icol) == null) {
                    newStr = "NULL";
                } else if ("null".equalsIgnoreCase(rsTable.getString(icol))) {
                    newStr = "_" + rsTable.getString(icol) + "_";
                } else {
                    newStr = rsTable.getString(icol);
                }

                row.addElement("field").addAttribute("name", rsmd.getColumnName(icol)).addAttribute("value",
                        newStr);
            }
        }

        output.write(document);

        output.close();

        rsTable.close();

        stTable.close();
    } catch (Exception ex) {
        BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
    } finally {
        if (null != output) {
            try {
                output.close();
            } catch (Exception outex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, outex.getMessage());
            }
        }

        if (null != rsTable) {
            try {
                rsTable.close();
            } catch (Exception rsex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, rsex.getMessage());
            }
        }

        if (null != stTable) {
            try {
                stTable.close();
            } catch (Exception stex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, stex.getMessage());
            }
        }
    }
}

From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void storeCertainDomainTable(String strTableName, String strPath, HmDomain oDomain,
        Connection conTable) {//from   w w  w. j  a v  a 2s  .c  o  m
    String strSql = "select * from " + strTableName + " where domainname=" + "'" + oDomain.getDomainName()
            + "'";

    XMLWriter output = null;

    ResultSet rsTable = null;

    Statement stTable = null;

    try {
        stTable = conTable.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

        rsTable = stTable.executeQuery(strSql);

        Document document = DocumentHelper.createDocument();

        Element table = document.addElement("table").addAttribute("schema", strTableName);

        File file = new File(strPath + strTableName.toLowerCase() + ".xml");

        output = new XMLWriter(new FileOutputStream(file));

        ResultSetMetaData rsmd = rsTable.getMetaData();

        int iCount = rsmd.getColumnCount();

        while (rsTable.next()) {
            Element row = table.addElement("row");

            for (int icol = 1; icol <= iCount; icol++) {
                String newStr;

                if (rsTable.getString(icol) == null) {
                    newStr = "NULL";
                } else if ("null".equalsIgnoreCase(rsTable.getString(icol))) {
                    newStr = "_" + rsTable.getString(icol) + "_";
                } else {
                    newStr = rsTable.getString(icol);
                }

                row.addElement("field").addAttribute("name", rsmd.getColumnName(icol)).addAttribute("value",
                        newStr);
            }
        }

        output.write(document);

        output.close();

        rsTable.close();

        stTable.close();
    } catch (Exception ex) {
        BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
    } finally {
        if (null != output) {
            try {
                output.close();
            } catch (Exception outex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, outex.getMessage());
            }
        }

        if (null != rsTable) {
            try {
                rsTable.close();
            } catch (Exception rsex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, rsex.getMessage());
            }
        }

        if (null != stTable) {
            try {
                stTable.close();
            } catch (Exception stex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, stex.getMessage());
            }
        }
    }
}

From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void storeCommonTable(String strTableName, String strPath, HmDomain oDomain, Connection conTable) {
    XMLWriter output = null;

    ResultSet rsTable = null;//from ww w.  j  av a  2 s .  c o  m

    Statement stTable = null;

    try {
        stTable = conTable.createStatement();

        boolean bOwnerFlag = false;

        String strSql = "select * from " + strTableName + " limit 1";

        rsTable = stTable.executeQuery(strSql);

        rsTable.next();

        ResultSetMetaData rsmd = rsTable.getMetaData();

        int iColumnCount = rsmd.getColumnCount();

        for (int i = 1; i <= iColumnCount; ++i) {
            if (rsmd.getColumnName(i).equalsIgnoreCase("owner")) {
                bOwnerFlag = true;

                break;
            }
        }

        rsTable.close();

        //         if (!bOwnerFlag) {
        //            strSql = "select count(*) from " + strTableName;
        //         } else {
        //            strSql = "select count(*) from " + strTableName
        //                  + ", hm_domain where " + strTableName
        //                  + ".owner=hm_domain.id and (" + strTableName
        //                  + ".owner=" + oDomain.getId().toString()
        //                  + " or hm_domain.domainname='global')";
        //         }

        //         rsTable = stTable.executeQuery(strSql);

        //         rsTable.next();

        //         int iRowCount = rsTable.getInt(1);

        //         rsTable.close();

        int intFileCount = 0;
        int intRecordNum;

        strSql = "select id from hm_domain where domainname='global'";
        rsTable = stTable.executeQuery(strSql);
        long global_id = 0;

        if (rsTable.next())
            global_id = rsTable.getLong(1);

        String strId = String.valueOf(global_id);

        while (true) {

            intRecordNum = 0;
            //for (int i = 0; i < iRowCount || i == 0; i = i + 5000) {

            //            if (!bOwnerFlag) {
            //               strSql = "select * from " + strTableName + " limit " + 5000
            //                     + " offset " + i;
            //            } else {
            //               strSql = "select " + strTableName + ".* from "
            //                     + strTableName + ", hm_domain where "
            //                     + strTableName + ".owner=hm_domain.id and ("
            //                     + strTableName + ".owner="
            //                     + oDomain.getId().toString()
            //                     + " or hm_domain.domainname='global')" + " limit "
            //                     + 5000 + " offset " + i;
            //            }

            if (!bOwnerFlag) {
                strSql = "select * from " + strTableName + " limit " + 5000 + " offset " + intFileCount * 5000;
            } else {
                strSql = "select * from " + strTableName + " where owner=" + oDomain.getId().toString()
                        + " or owner=" + strId + " limit " + 5000 + " offset " + intFileCount * 5000;
            }

            rsTable = stTable.executeQuery(strSql);

            Document document = DocumentHelper.createDocument();

            Element table = document.addElement("table").addAttribute("schema", strTableName);

            rsmd = rsTable.getMetaData();

            int iCount = rsmd.getColumnCount();

            while (rsTable.next()) {
                intRecordNum++;
                Element row = table.addElement("row");

                for (int icol = 1; icol <= iCount; icol++) {
                    String newStr;

                    if (rsTable.getString(icol) == null) {
                        newStr = "NULL";
                    } else if ("null".equalsIgnoreCase(rsTable.getString(icol))) {
                        newStr = "_" + rsTable.getString(icol) + "_";
                    } else {
                        newStr = rsTable.getString(icol);
                    }

                    //                  row.addElement("field").addAttribute("name",
                    //                        rsmd.getColumnName(icol)).addAttribute("value",
                    //                        newStr);
                    if (1 == intRecordNum) {
                        row.addElement("field").addAttribute("name", rsmd.getColumnName(icol))
                                .addAttribute("value", newStr);
                    } else {
                        row.addElement("field").addAttribute("value", newStr);
                    }
                }
            }

            if (intRecordNum <= 0 && 0 != intFileCount)
                break;

            File file;

            if (intFileCount == 0) {
                file = new File(strPath + strTableName.toLowerCase() + ".xml");

            } else {
                file = new File(strPath + strTableName.toLowerCase() + "_" + intFileCount + ".xml");
            }

            intFileCount++;

            output = new XMLWriter(new FileOutputStream(file));

            output.write(document);

            output.close();

            if (5000 > intRecordNum)
                break;
        }

        rsTable.close();

        stTable.close();
    } catch (Exception ex) {
        BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
    } finally {
        if (null != output) {
            try {
                output.close();
            } catch (Exception outex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, outex.getMessage());
            }
        }

        if (null != rsTable) {
            try {
                rsTable.close();
            } catch (Exception rsex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, rsex.getMessage());
            }
        }

        if (null != stTable) {
            try {
                stTable.close();
            } catch (Exception stex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, stex.getMessage());
            }
        }
    }
}

From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void storeSpecialTable(String strTableName, String strPath, HmDomain oDomain, Connection conTable) {
    XMLWriter output = null;

    ResultSet rsTable = null;// w  ww . jav a  2  s.c o  m

    Statement stTable = null;

    try {
        if (!(strPath.substring(strPath.length() - 1).equals(File.separator)
                || strPath.substring(strPath.length() - 1).equals("/"))) {
            strPath = strPath + File.separatorChar;
        }

        stTable = conTable.createStatement();

        //         String strSql = "select count(*) from " + strTableName
        //               + ", hm_domain where " + strTableName
        //               + ".owner=hm_domain.id and (" + strTableName + ".owner="
        //               + oDomain.getId().toString()
        //               + " or hm_domain.domainname='global')";
        //
        //         begin = System.currentTimeMillis();
        //
        //         rsTable = stTable.executeQuery(strSql);
        //
        //         dbtime += System.currentTimeMillis() - begin;
        //
        //         rsTable.next();
        //
        //         int iRowCount = rsTable.getInt(1);
        //
        //         rsTable.close();

        String strSql = "select id from " + strTableName + " where owner=" + oDomain.getId().toString()
                + " order by id asc limit 1";

        rsTable = stTable.executeQuery(strSql);

        long id = 0;
        if (rsTable.next())
            id = rsTable.getLong(1) - 1;
        //         else
        //            return;

        rsTable.close();

        int intFileCount = 0;
        String strId = String.valueOf(id);
        int intRecordNum;

        while (true) {
            intRecordNum = 0;
            //            strSql = "select * from " + strTableName
            //                  + " where owner=" + oDomain.getId().toString()
            //                  + " and id>" + strId +" order by id asc "+ " limit " + 5000;

            strSql = "select * from " + strTableName + " where id > " + strId + " and owner="
                    + oDomain.getId().toString() + " order by id asc " + " limit " + 5000;

            rsTable = stTable.executeQuery(strSql);

            Document document = DocumentHelper.createDocument();

            Element table = document.addElement("table").addAttribute("schema", strTableName);

            ResultSetMetaData rsmd = rsTable.getMetaData();

            int iCount = rsmd.getColumnCount();

            while (rsTable.next()) {
                intRecordNum++;
                Element row = table.addElement("row");

                for (int icol = 1; icol <= iCount; icol++) {
                    String newStr;

                    if (rsTable.getString(icol) == null) {
                        newStr = "NULL";
                    } else if ("null".equalsIgnoreCase(rsTable.getString(icol))) {
                        newStr = "_" + rsTable.getString(icol) + "_";
                    } else {
                        newStr = rsTable.getString(icol);
                    }

                    if ("id".equalsIgnoreCase(rsmd.getColumnName(icol))) {
                        strId = newStr;
                    }

                    if (1 == intRecordNum) {
                        row.addElement("field").addAttribute("name", rsmd.getColumnName(icol))
                                .addAttribute("value", newStr);
                    } else {
                        row.addElement("field").addAttribute("value", newStr);
                    }
                }
            }

            if (intRecordNum <= 0 && 0 != intFileCount)
                break;

            File file;

            if (intFileCount == 0) {
                file = new File(strPath + strTableName.toLowerCase() + ".xml");

            } else {
                file = new File(strPath + strTableName.toLowerCase() + "_" + intFileCount + ".xml");
            }
            intFileCount++;

            output = new XMLWriter(new FileOutputStream(file));

            output.write(document);

            output.close();

            if (5000 > intRecordNum)
                break;
        }

        rsTable.close();

        stTable.close();
    } catch (Exception ex) {
        BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
    } finally {
        if (null != output) {
            try {
                output.close();
            } catch (Exception outex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, outex.getMessage());
            }
        }

        if (null != rsTable) {
            try {
                rsTable.close();
            } catch (Exception rsex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, rsex.getMessage());
            }
        }

        if (null != stTable) {
            try {
                stTable.close();
            } catch (Exception stex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, stex.getMessage());
            }
        }
    }
}

From source file:com.ai.tools.generator.util.XMLFormatter.java

License:Open Source License

public static String toString(Branch branch, String indent, boolean expandEmptyElements) throws IOException {
    ByteArrayMaker bam = new ByteArrayMaker();

    OutputFormat format = OutputFormat.createPrettyPrint();

    format.setExpandEmptyElements(expandEmptyElements);
    format.setIndent(indent);/*from   w  ww.ja  va2  s  .co  m*/
    format.setLineSeparator("\n");

    XMLWriter writer = new XMLWriter(bam, format);

    writer.write(branch);

    String content = bam.toString(StringPool.UTF8);

    // LEP-4257

    //content = StringUtil.replace(content, "\n\n\n", "\n\n");
    if (content.endsWith("\n\n")) {
        content = content.substring(0, content.length() - 2);
    }

    if (content.endsWith("\n")) {
        content = content.substring(0, content.length() - 1);
    }

    while (content.indexOf(" \n") != -1) {
        content = StringUtil.replace(content, " \n", "\n");
    }

    return content;
}

From source file:com.alibaba.citrus.springext.support.SchemaUtil.java

License:Open Source License

/** DOM */
public static void writeDocument(Document doc, Writer writer, String charset) throws IOException {
    charset = defaultIfEmpty(trimToNull(charset), "UTF-8");

    OutputFormat format = OutputFormat.createPrettyPrint();

    format.setEncoding(charset);/*  w ww  .j  a  v  a2s. c  o m*/
    format.setIndent(true);
    format.setIndentSize(4);

    XMLWriter xmlWriter = new XMLWriter(writer, format);
    xmlWriter.write(doc);
    xmlWriter.flush();
}

From source file:com.alibaba.citrus.springext.util.ConvertToUnqualifiedStyle.java

License:Open Source License

private static void writeDocument(Document doc, OutputStream stream) throws IOException {
    String charset = "UTF-8";
    Writer writer = new OutputStreamWriter(stream, charset);

    OutputFormat format = new OutputFormat();

    format.setEncoding(charset);//from www. java2 s.co  m

    XMLWriter xmlWriter = new XMLWriter(writer, format);
    xmlWriter.write(doc);
    xmlWriter.flush();
}

From source file:com.amalto.workbench.utils.MDMServerHelper.java

License:Open Source License

private static boolean saveRootElement(Element rootElement) {
    try {//from  w  w  w.j a v  a 2 s . c  o m
        XMLWriter writer = new XMLWriter(new FileWriter(MDMServerHelper.workbenchConfigFile));
        writer.write(rootElement.getDocument());
        writer.close();
        return true;
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        return false;
    }
}