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();
}
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));
}
}
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);
}
}
}
}
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);
}
}
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);
}
}
Aggregations