use of org.dom4j.Document in project reflections by ronmamo.
the class XmlSerializer method toString.
public String toString(final Reflections reflections) {
Document document = createDocument(reflections);
try {
StringWriter writer = new StringWriter();
XMLWriter xmlWriter = new XMLWriter(writer, OutputFormat.createPrettyPrint());
xmlWriter.write(document);
xmlWriter.close();
return writer.toString();
} catch (IOException e) {
throw new RuntimeException();
}
}
use of org.dom4j.Document in project eweb4j-framework by laiweiwei.
the class BeanXMLWriter method createDoc.
private Document createDoc() throws Exception {
Document doc = DocumentHelper.createDocument();
if ((this.rootElementName == null || this.rootElementName.trim().length() == 0) && this.list.size() == 1) {
String name;
if (this.beanName == null || this.beanName.trim().length() == 0)
name = BeanXMLConstant.SUBROOT_ELEMENT;
else
name = this.beanName;
Element bean = doc.addElement(name);
for (Object t : this.list) {
// 递归
writeRecursion(bean, t);
}
} else {
if (this.rootElementName == null || this.rootElementName.trim().length() == 0)
this.rootElementName = BeanXMLConstant.ROOT_ELEMENT;
Element beans = doc.addElement(this.rootElementName);
Element bean;
String sub;
if (this.beanName == null || this.beanName.trim().length() == 0)
sub = BeanXMLConstant.SUBROOT_ELEMENT;
else
sub = this.beanName;
for (Object t : this.list) {
if (this.isSubNameAuto)
bean = beans.addElement(t.getClass().getSimpleName().toLowerCase());
else
bean = beans.addElement(sub);
// 递归
writeRecursion(bean, t);
}
}
return doc;
}
use of org.dom4j.Document in project eweb4j-framework by laiweiwei.
the class BeanXMLWriter method write.
public File write() throws Exception {
Document doc = createDoc();
// 读取文件
FileOutputStream fos = new FileOutputStream(this.file);
// 设置文件编码
OutputFormat format = OutputFormat.createPrettyPrint();
// 创建写文件方法
org.dom4j.io.XMLWriter xmlWriter = new org.dom4j.io.XMLWriter(fos, format);
// 写入文件
xmlWriter.write(doc);
// 关闭
fos.close();
xmlWriter.close();
return this.file;
}
use of org.dom4j.Document in project eweb4j-framework by laiweiwei.
the class XmlUtil method Str2Map.
public static Map<String, Object> Str2Map(String str) throws DocumentException {
Map<String, Object> map = new HashMap<String, Object>();
if (str == null)
return map;
Document doc = DocumentHelper.parseText(str);
Element root = doc.getRootElement();
for (Iterator iterator = root.elementIterator(); iterator.hasNext(); ) {
Element e = (Element) iterator.next();
// System.out.println(e.getName());
List list = e.elements();
if (list.size() > 0) {
map.put(e.getName(), Dom2Map(e));
} else
map.put(e.getName(), e.getText());
}
return map;
}
use of org.dom4j.Document in project gradle by gradle.
the class DOM4JSerializer method exportToFile.
public static void exportToFile(ExportInteraction exportInteraction, ExtensionFileFilter fileFilter, DOM4JSettingsNode settingsNode) {
File file = promptForFile(exportInteraction, fileFilter);
if (file == null) {
//the user canceled.
return;
}
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
LOGGER.error("Could not write to file: " + file.getAbsolutePath(), e);
exportInteraction.reportError("Could not write to file: " + file.getAbsolutePath());
return;
}
try {
XMLWriter xmlWriter = new XMLWriter(fileOutputStream, OutputFormat.createPrettyPrint());
Element rootElement = settingsNode.getElement();
rootElement.detach();
Document document = DocumentHelper.createDocument(rootElement);
xmlWriter.write(document);
} catch (Throwable t) {
LOGGER.error("Internal error. Failed to save.", t);
exportInteraction.reportError("Internal error. Failed to save.");
} finally {
closeQuietly(fileOutputStream);
}
}
Aggregations