Example usage for org.dom4j.io XMLWriter XMLWriter

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

Introduction

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

Prototype

public XMLWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException 

Source Link

Usage

From source file:com.uletian.ultcrm.business.service.CustomerInfoSyncService.java

public void notifycationDataChange(Customer customer) {
    StringWriter writer = new StringWriter();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");
    Document doc = DocumentHelper.createDocument();

    Namespace namespace = new Namespace("ns0", "http://crm/91jpfw.cn");
    Element root = doc.addElement(new QName("customer", namespace));
    root.addElement(new QName("action")).addText(action.BINDING_TEL.toString());
    root.addElement(new QName("sourceSys")).addText("ULTCRM");
    root.addElement(new QName("ultcrmid")).addText(customer.getId().toString());
    root.addElement(new QName("crmid")).addText(customer.getSyncid() == null ? "" : customer.getSyncid());
    String name = null;/*from  w w w.j a v a  2 s  .com*/
    if (customer.getName() == null || "".equals(customer.getName())) {
        name = customer.getNickname();
    } else {
        name = customer.getName();
    }
    root.addElement(new QName("name")).addText(name);
    root.addElement(new QName("sexy")).addText(customer.getSex() == null ? "" : customer.getSex());
    root.addElement(new QName("telephone")).addText(customer.getPhone() == null ? "" : customer.getPhone());
    root.addElement(new QName("country")).addText(customer.getCountry() == null ? "" : customer.getCountry());
    root.addElement(new QName("province"))
            .addText(customer.getProvince() == null ? "" : customer.getProvince());
    root.addElement(new QName("city")).addText(customer.getCity() == null ? "" : customer.getCity());
    root.addElement(new QName("address")).addText(customer.getAddress() == null ? "" : customer.getAddress());
    root.addElement(new QName("postcode"))
            .addText(customer.getPostcode() == null ? "" : customer.getPostcode());
    Element techsElement = root.addElement(new QName("techs"));
    List<Tech> techs = customer.getTechs();
    if (techs != null && techs.size() > 0) {
        for (int i = 0; i < techs.size(); i++) {
            Tech tech = techs.get(i);
            Element techElement = techsElement.addElement("tech");

            techElement.addElement(new QName("crmtechid"))
                    .addText(tech.getCrmTechId() == null ? "" : tech.getCrmTechId());
            techElement.addElement(new QName("code")).addText(tech.getTechModel().getCode());
            techElement.addElement(new QName("techlevelno"))
                    .addText(tech.getTechlevelno() == null ? "" : tech.getTechlevelno());
            techElement.addElement(new QName("techerno"))
                    .addText(tech.getTecherno() == null ? "" : tech.getTecherno());
            techElement.addElement(new QName("techname"))
                    .addText(tech.getTechname() == null ? "" : tech.getTechname());
            techElement.addElement(new QName("coursetime"))
                    .addText(tech.getCoursetime() == null ? "" : tech.getCoursetime());
            String trainExpireDate = "";
            if (tech.getTrainExpireDate() != null) {
                trainExpireDate = sdf.format(tech.getTrainExpireDate());
            }
            techElement.addElement(new QName("trainExpireDate")).addText(trainExpireDate);
            techElement.addElement(new QName("trainCompany"))
                    .addText(tech.getTrainCompany() == null ? "" : tech.getTrainCompany());
            techElement.addElement(new QName("courseCode"))
                    .addText(tech.getCourseCode() == null ? "" : tech.getCourseCode());
            techElement.addElement(new QName("techColor"))
                    .addText(tech.getColor() == null ? "" : tech.getColor());
            String registerDate = "";
            if (tech.getRegisterDate() != null) {
                registerDate = sdf.format(tech.getRegisterDate());
            }
            techElement.addElement(new QName("registerDate")).addText(registerDate);
            techElement.addElement(new QName("courseLicense"))
                    .addText(tech.getCourseLicense() == null ? "" : tech.getCourseLicense());
            String checkExpireDate = "";
            if (tech.getCheckExpireDate() != null) {
                checkExpireDate = sdf.format(tech.getCheckExpireDate());
            }
            techElement.addElement(new QName("checkExpireDate")).addText(checkExpireDate);
            techElement.addElement(new QName("memberLevel"))
                    .addText(tech.getMemberLevel() == null ? "" : tech.getMemberLevel());

            // ? by xiecheng 2015-11-19
            techElement.addElement(new QName("nextMaintCoursetime"))
                    .addText(StringUtils.isNoneBlank(tech.getNextMaintCoursetime())
                            ? tech.getNextMaintCoursetime()
                            : "");

            String nextMaintDate = "";
            if (tech.getNextMaintDate() != null) {
                nextMaintDate = sdf.format(tech.getNextMaintDate());
            }
            techElement.addElement(new QName("nextMaintDate")).addText(nextMaintDate);

            String lastConsumeDate = "";
            if (tech.getLastConsumeDate() != null) {
                lastConsumeDate = sdf.format(tech.getLastConsumeDate());
            }
            techElement.addElement(new QName("lastConsumeDate")).addText(lastConsumeDate);

        }
    }
    XMLWriter xmlwriter = new XMLWriter(writer, format);
    try {
        xmlwriter.write(doc);
    } catch (IOException e) {
    }
    customerInfoMessageService.sendMessage(writer.toString());

}

From source file:com.vmware.o11n.plugin.powershell.remote.impl.winrm.ClientState.java

License:Open Source License

private String toString(Document doc) {
    StringWriter stringWriter = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter(stringWriter, OutputFormat.createPrettyPrint());
    try {/*from  w w  w  .  j ava  2s  . c om*/
        xmlWriter.write(doc);
        xmlWriter.close();
    } catch (IOException e) {
        throw new WinRMRuntimeIOException("error ", e);
    }
    return stringWriter.toString();
}

From source file:com.webarch.common.io.xml.XMLEditor.java

License:Apache License

public boolean save(File outPutFile, Document document, boolean lineAble) {
    boolean flag = true;
    XMLWriter writer = null;/*w w  w .j av  a2 s  . c  o m*/
    OutputStreamWriter outputStream = null;
    try {
        outputStream = new OutputStreamWriter(new FileOutputStream(outPutFile), DAFAULT_CHARSET);
        final OutputFormat format = OutputFormat.createCompactFormat();//?
        format.setNewlines(lineAble);
        writer = new XMLWriter(outputStream, format);
        writer.write(document);
        writer.flush();
        outputStream.close();
        writer.close();
    } catch (Exception ex) {
        log.error("?xml", ex);
        flag = false;
    } finally {
        try {
            if (null != writer) {
                writer.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }

        } catch (IOException e) {
            log.error("?xml:", e);

        }
    }
    return flag;
}

From source file:com.wedian.site.common.utils.SettingUtils.java

License:Open Source License

/**
 * /*from  w w  w . j  a  v a  2 s. c  o m*/
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File shopxxXmlFile = new ClassPathResource(CommonAttributes.SITE_XML_PATH).getFile();
        Document document = new SAXReader().read(shopxxXmlFile);
        List<Element> elements = document.selectNodes("/site/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(shopxxXmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.weibo.wesync.notify.xml.XMLProperties.java

License:Open Source License

/**
 * Saves the properties to disk as an XML document. A temporary file is
 * used during the writing process for maximum safety.
 *///from   ww  w  .  java  2 s  .com
private synchronized void saveProperties() {
    boolean error = false;
    // Write data out to a temporary file first.
    File tempFile = null;
    Writer writer = null;
    try {
        tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile), "UTF-8"));
        OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
        xmlWriter.write(document);
    } catch (Exception e) {
        // There were errors so abort replacing the old property file.
        error = true;
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e1) {
                error = true;
            }
        }
    }

    // No errors occured, so delete the main file.
    if (!error) {
        // Delete the old file so we can replace it.
        if (!file.delete()) {
            return;
        }
        // Copy new contents to the file.
        try {
            copy(tempFile, file);
        } catch (Exception e) {
            // There were errors so abort replacing the old property file.
            error = true;
        }
        // If no errors, delete the temp file.
        if (!error) {
            tempFile.delete();
        }
    }
}

From source file:com.xebialabs.overthere.cifs.winrm.exception.WinRMRuntimeIOException.java

License:Open Source License

private String toString(Document doc) {
    if (doc == null) {
        return "[EMPTY]";
    }/* ww w .  ja v a  2  s . com*/

    StringWriter stringWriter = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter(stringWriter, OutputFormat.createPrettyPrint());
    try {
        xmlWriter.write(doc);
        xmlWriter.close();
    } catch (IOException e) {
        throw new RuntimeException("error ", e);
    }
    return stringWriter.toString();
}

From source file:com.xebialabs.overthere.cifs.winrm.soap.Soapy.java

License:Open Source License

public static String toString(Document doc) {
    StringWriter stringWriter = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter(stringWriter, OutputFormat.createPrettyPrint());
    try {//from w w  w  . j  av  a 2  s  . c  o m
        xmlWriter.write(doc);
        xmlWriter.close();
    } catch (IOException e) {
        throw new WinRmRuntimeIOException("Cannnot convert XML to String ", e);
    }
    return stringWriter.toString();
}

From source file:com.xebialabs.overthere.cifs.winrm.WinRmRuntimeIOException.java

License:Open Source License

private static String toString(Document doc) {
    if (doc == null) {
        return "[EMPTY]";
    }//from  w  ww . j  a v  a 2 s. c  o  m

    StringWriter stringWriter = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter(stringWriter, OutputFormat.createPrettyPrint());
    try {
        xmlWriter.write(doc);
        xmlWriter.close();
    } catch (IOException e) {
        throw new RuntimeException("error ", e);
    }
    return stringWriter.toString();
}

From source file:com.xebialabs.overthere.winrm.WinRmClient.java

License:Open Source License

private static void logDocument(String caption, final Document document) {
    if (!logger.isTraceEnabled()) {
        return;// www. ja v  a2s  . c om
    }

    StringWriter text = new StringWriter();
    try {
        XMLWriter writer = new XMLWriter(text, OutputFormat.createPrettyPrint());
        writer.write(document);
        writer.close();
    } catch (IOException e) {
        logger.trace("{}\n{}", caption, e);
    }

    logger.trace("{}\n{}", caption, text);
}

From source file:com.xebialabs.overthere.winrm.WinRmClient.java

License:Open Source License

private static String toString(Document doc) {
    StringWriter stringWriter = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter(stringWriter, OutputFormat.createPrettyPrint());
    try {//from  w  ww. j  a v  a 2  s  . co m
        xmlWriter.write(doc);
        xmlWriter.close();
    } catch (IOException exc) {
        throw new WinRmRuntimeIOException("Cannnot convert XML to String ", exc);
    }
    return stringWriter.toString();
}