Search in sources :

Example 21 with Document

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();
    }
}
Also used : Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter)

Example 22 with Document

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;
}
Also used : Element(org.dom4j.Element) Document(org.dom4j.Document)

Example 23 with Document

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;
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputFormat(org.dom4j.io.OutputFormat) Document(org.dom4j.Document)

Example 24 with Document

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;
}
Also used : HashMap(java.util.HashMap) Element(org.dom4j.Element) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.dom4j.Document)

Example 25 with Document

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);
    }
}
Also used : Element(org.dom4j.Element) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter)

Aggregations

Document (org.dom4j.Document)891 Element (org.dom4j.Element)492 SAXReader (org.dom4j.io.SAXReader)252 File (java.io.File)135 IOException (java.io.IOException)135 StringReader (java.io.StringReader)111 ArrayList (java.util.ArrayList)110 List (java.util.List)107 Test (org.junit.Test)101 DocumentException (org.dom4j.DocumentException)93 HashMap (java.util.HashMap)90 InputStream (java.io.InputStream)82 Node (org.dom4j.Node)80 Test (org.junit.jupiter.api.Test)80 XMLWriter (org.dom4j.io.XMLWriter)53 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)48 FileInputStream (java.io.FileInputStream)45 Map (java.util.Map)41 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)40 XMLParser (org.olat.core.util.xml.XMLParser)40