Search in sources :

Example 76 with OutputFormat

use of org.dom4j.io.OutputFormat in project wechat by dllwh.

the class XmlUtils method createXml.

public static String createXml(Map<?, ?> map) throws Exception {
    // 创建root
    Element root = DocumentHelper.createDocument().addElement("xml");
    for (Object key : map.keySet()) {
        // 生成root的一个接点
        Element param = root.addElement(String.valueOf(key));
        // 为节点添加文本, 也可以用addText()
        param.addCDATA(String.valueOf(map.get(key)));
    }
    OutputFormat xmlFormat = new OutputFormat();
    // 设置文件编码
    xmlFormat.setEncoding("UTF-8");
    // 设置换行
    xmlFormat.setNewlines(false);
    // 生成缩进
    xmlFormat.setIndent(false);
    // 使用4个空格进行缩进, 可以兼容文本编辑器
    // xmlFormat.setIndent(" ");
    // 创建写文件方法
    XMLWriter xmlWriter = new XMLWriter(xmlFormat);
    /**
     * xmlWriter.write(document);
     */
    xmlWriter.write(root);
    xmlWriter.close();
    return xmlWriter.toString();
}
Also used : Element(org.dom4j.Element) OutputFormat(org.dom4j.io.OutputFormat) XMLWriter(org.dom4j.io.XMLWriter)

Example 77 with OutputFormat

use of org.dom4j.io.OutputFormat in project new-cloud by xie-summer.

the class XmlUtils method formatXml.

/**
 * 直接写入,不关闭writer
 * @param str
 * @param encoding
 * @param writer
 * @throws IOException
 */
public static void formatXml(String str, String encoding, Writer writer) throws IOException {
    OutputFormat format = OutputFormat.createCompactFormat();
    format.setEncoding(encoding);
    XMLWriter xmlWriter = new XMLWriter(writer, format);
    try {
        org.dom4j.Document document = DocumentHelper.parseText(str);
        xmlWriter.write(document);
    } catch (DocumentException e) {
        DB_LOGGER.warn(str + LoggerUtils.getExceptionTrace(e, 100));
    }
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) XMLWriter(org.dom4j.io.XMLWriter) org.dom4j(org.dom4j)

Example 78 with OutputFormat

use of org.dom4j.io.OutputFormat in project Openfire by igniterealtime.

the class UpdateManager method saveAvailablePluginsInfo.

/**
 * Saves to conf/available-plugins.xml the list of plugins that are available
 * at igniterealtime.org.
 */
private void saveAvailablePluginsInfo() {
    // XML to store in the file
    Element xml = docFactory.createDocument().addElement("available");
    for (AvailablePlugin plugin : availablePlugins.values()) {
        Element component = xml.addElement("plugin");
        component.addAttribute("name", plugin.getName());
        component.addAttribute("latest", plugin.getVersion() != null ? plugin.getVersion().getVersionString() : null);
        component.addAttribute("changelog", plugin.getChangelog() != null ? plugin.getChangelog().toExternalForm() : null);
        component.addAttribute("url", plugin.getDownloadURL() != null ? plugin.getDownloadURL().toExternalForm() : null);
        component.addAttribute("author", plugin.getAuthor());
        component.addAttribute("description", plugin.getDescription());
        component.addAttribute("icon", plugin.getIcon() != null ? plugin.getIcon().toExternalForm() : null);
        component.addAttribute("minServerVersion", plugin.getMinServerVersion() != null ? plugin.getMinServerVersion().getVersionString() : null);
        component.addAttribute("priorToServerVersion", plugin.getPriorToServerVersion() != null ? plugin.getPriorToServerVersion().getVersionString() : null);
        component.addAttribute("readme", plugin.getReadme() != null ? plugin.getReadme().toExternalForm() : null);
        component.addAttribute("licenseType", plugin.getLicense());
        component.addAttribute("fileSize", Long.toString(plugin.getFileSize()));
    }
    // Write data out to conf/available-plugins.xml file.
    Writer writer = null;
    try {
        // Create the conf folder if required
        File file = new File(JiveGlobals.getHomeDirectory(), "conf");
        if (!file.exists()) {
            file.mkdir();
        }
        file = new File(JiveGlobals.getHomeDirectory() + File.separator + "conf", "available-plugins.xml");
        // Delete the old version.xml file if it exists
        if (file.exists()) {
            file.delete();
        }
        // Create new version.xml with returned data
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
        OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
        xmlWriter.write(xml);
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e1) {
                Log.error(e1.getMessage(), e1);
            }
        }
    }
}
Also used : Element(org.dom4j.Element) OutputFormat(org.dom4j.io.OutputFormat) MalformedURLException(java.net.MalformedURLException) ExecutionException(java.util.concurrent.ExecutionException)

Example 79 with OutputFormat

use of org.dom4j.io.OutputFormat in project Openfire by igniterealtime.

the class UpdateManager method saveLatestServerInfo.

/**
 * Saves to conf/server-update.xml information about the latest Openfire release that is
 * available for download.
 */
private void saveLatestServerInfo() {
    Element xmlResponse = docFactory.createDocument().addElement("version");
    if (serverUpdate != null) {
        Element component = xmlResponse.addElement("openfire");
        component.addAttribute("latest", serverUpdate.getLatestVersion());
        component.addAttribute("changelog", serverUpdate.getChangelog());
        component.addAttribute("url", serverUpdate.getURL());
    }
    // Write data out to conf/server-update.xml file.
    try {
        // Create the conf folder if required
        File file = new File(JiveGlobals.getHomeDirectory(), "conf");
        if (!file.exists()) {
            file.mkdir();
        }
        file = new File(JiveGlobals.getHomeDirectory() + File.separator + "conf", "server-update.xml");
        // Delete the old server-update.xml file if it exists
        if (file.exists()) {
            file.delete();
        }
        // Create new version.xml with returned data
        try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) {
            OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
            XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
            xmlWriter.write(xmlResponse);
        }
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    }
}
Also used : Element(org.dom4j.Element) OutputFormat(org.dom4j.io.OutputFormat) MalformedURLException(java.net.MalformedURLException) ExecutionException(java.util.concurrent.ExecutionException)

Example 80 with OutputFormat

use of org.dom4j.io.OutputFormat in project openolat by klemens.

the class QTIExportProcessor method addMetadata.

private void addMetadata(QuestionItemFull fullItem, String dir, ZipOutputStream zout) {
    try {
        Document document = DocumentHelper.createDocument();
        Element qtimetadata = document.addElement("qtimetadata");
        QTIMetadataConverter enricher = new QTIMetadataConverter(qtimetadata);
        enricher.toXml(fullItem);
        zout.putNextEntry(new ZipEntry(dir + "/" + "qitem_" + fullItem.getKey() + "_metadata.xml"));
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(zout, format);
        writer.write(document);
    } catch (IOException e) {
        log.error("", e);
    }
}
Also used : Element(org.dom4j.Element) ZipEntry(java.util.zip.ZipEntry) OutputFormat(org.dom4j.io.OutputFormat) IOException(java.io.IOException) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter)

Aggregations

OutputFormat (org.dom4j.io.OutputFormat)89 XMLWriter (org.dom4j.io.XMLWriter)75 IOException (java.io.IOException)39 Document (org.dom4j.Document)31 Element (org.dom4j.Element)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)18 StringWriter (java.io.StringWriter)18 SAXReader (org.dom4j.io.SAXReader)16 File (java.io.File)15 FileOutputStream (java.io.FileOutputStream)14 FileWriter (java.io.FileWriter)10 OutputStreamWriter (java.io.OutputStreamWriter)8 StringReader (java.io.StringReader)6 DocumentException (org.dom4j.DocumentException)6 OutputStream (java.io.OutputStream)5 Test (org.junit.Test)5 BufferedOutputStream (java.io.BufferedOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 MalformedURLException (java.net.MalformedURLException)4 ArrayList (java.util.ArrayList)4