Example usage for org.dom4j.io XMLWriter write

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

Introduction

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

Prototype

public void write(Object object) throws IOException 

Source Link

Document

Writes the given object which should be a String, a Node or a List of Nodes.

Usage

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

License:Apache License

private void writeDocument(final OutputStream out, Document document) {
    XMLWriter xmlWriter = null;
    try {/*  w  w  w. j a v  a2 s .  co m*/
        xmlWriter = new XMLWriter(out);
        xmlWriter.write(document);
        xmlWriter.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }

}

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

License:Open Source License

/**
 * /*w w w.j  a v a2  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.
 *//* w ww.  ja v a 2  s. c o  m*/
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]";
    }/*w  w  w  . j a  va 2s  .c  om*/

    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 {// w  w w .  j a va  2s .  co 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   ww w. j  av  a2s .c  om*/

    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;//w w w  .  j  a  v a2  s .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  av a2  s . c o  m*/
        xmlWriter.write(doc);
        xmlWriter.close();
    } catch (IOException exc) {
        throw new WinRmRuntimeIOException("Cannnot convert XML to String ", exc);
    }
    return stringWriter.toString();
}

From source file:com.xpn.xwiki.objects.BaseCollection.java

License:Open Source License

public String toXMLString() {
    Document doc = new DOMDocument();
    doc.setRootElement(toXML(null));/*  w w w.j av  a2  s.c o  m*/
    OutputFormat outputFormat = new OutputFormat("", true);
    StringWriter out = new StringWriter();
    XMLWriter writer = new XMLWriter(out, outputFormat);
    try {
        writer.write(doc);
        return out.toString();
    } catch (IOException e) {
        e.printStackTrace();

        return "";
    }
}

From source file:com.xpn.xwiki.objects.BaseElement.java

License:Open Source License

protected void fromXML(Element oel) throws XWikiException {
    // Serialize the Document (could not find a way to convert a dom4j Element into a usable StAX source)
    StringWriter writer = new StringWriter();
    try {/*from   ww  w.  ja  v  a2 s . c o m*/
        org.dom4j.io.XMLWriter domWriter = new org.dom4j.io.XMLWriter(writer);
        domWriter.write(oel);
        domWriter.flush();
    } catch (IOException e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_DOC, XWikiException.ERROR_DOC_XML_PARSING,
                "Error parsing xml", e, null);
    }

    // Actually parse the XML
    fromXML(writer.toString());
}