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.amalto.workbench.utils.Util.java

License:Open Source License

public static String formatXsdSource(String xsdSource, boolean suppressDeclaration) {
    try {/*from w  w  w . ja v a 2s.co  m*/
        SAXReader reader = new SAXReader();
        org.dom4j.Document document = reader.read(new StringReader(xsdSource));
        StringWriter writer = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");//$NON-NLS-1$
        format.setSuppressDeclaration(suppressDeclaration);
        XMLWriter xmlwriter = new XMLWriter(writer, format);
        xmlwriter.write(document);
        String str = writer.toString();
        writer.close();
        xmlwriter.close();
        return str;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return xsdSource;

}

From source file:com.amalto.workbench.utils.XmlUtil.java

License:Open Source License

public static void write(Document document, String filePath, String printMode, String encoding)
        throws IOException {

    OutputFormat format = null;/*from  www  . j ava2s  . c  om*/

    if (printMode.toLowerCase().equals("pretty")) {//$NON-NLS-1$
        // Pretty print the document
        format = OutputFormat.createPrettyPrint();
    } else if (printMode.toLowerCase().equals("compact")) {//$NON-NLS-1$
        // Compact format
        format = OutputFormat.createCompactFormat();
    }

    format.setEncoding(encoding);

    // lets write to a file
    XMLWriter writer = new XMLWriter(new FileOutputStream(filePath), format);

    // XMLWriter logger = new XMLWriter( System.out, format );

    writer.write(document);

    logger.info(Messages.bind(Messages.XmlUtil_Loginfo1, filePath));

    // logger.write( document );

    // logger.close();

    writer.close();
}

From source file:com.amalto.workbench.utils.XmlUtil.java

License:Open Source License

public static String format(Document document, OutputFormat format, String encoding) {

    StringWriter writer = new StringWriter();

    format.setEncoding(encoding);/*from   w  ww . j a  va 2 s. c  o  m*/

    format.setNewLineAfterDeclaration(false);
    // format.setSuppressDeclaration(suppressDeclaration);

    XMLWriter xmlwriter = new XMLWriter(writer, format);

    String result = ""; //$NON-NLS-1$

    try {

        xmlwriter.write(document);
        result = writer.toString().replaceAll("<\\?xml.*?\\?>", "").trim();//$NON-NLS-1$//$NON-NLS-2$
    } catch (Exception e) {
        log.error(e.getMessage(), e);

    } finally {
        try {
            if (xmlwriter != null)
                xmlwriter.close();

            if (writer != null)
                writer.close();
        } catch (IOException e) {

        }
    }

    return result;
}

From source file:com.amalto.workbench.utils.XmlUtil.java

License:Open Source License

public static String formatXmlSource(String xmlSource) {

    SAXReader reader = new SAXReader();
    StringReader stringReader = new StringReader(xmlSource);
    StringWriter writer = null;//from  w  ww.  j  a v  a2  s  .  com
    XMLWriter xmlwriter = null;
    String result = xmlSource;

    try {
        Document document = reader.read(stringReader);
        writer = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");//$NON-NLS-1$
        format.setIndentSize(4);
        format.setSuppressDeclaration(true);
        xmlwriter = new XMLWriter(writer, format);
        xmlwriter.write(document);
        result = writer.toString();
    } catch (Exception e) {

    } finally {

        try {
            if (stringReader != null)
                stringReader.close();

            if (xmlwriter != null)
                xmlwriter.close();

            if (writer != null)
                writer.close();

        } catch (Exception e) {

        }

    }
    return result;

}

From source file:com.anite.zebra.ext.xmlloader.XMLLoadProcess.java

License:Apache License

/**
 * Read in a file of XML, strip out the pretty printing via some juggling of objects and then return a
 * org.w3.document DOM object./*from  www .j  a  va 2 s . c  om*/
 * 
 * @param xmlFile
 *            The file to be read in.
 * @return A ready to use org.w3.Document with pretty printing stripped out.
 * @throws DocumentException
 * @throws MalformedURLException
 * @throws UnsupportedEncodingException
 * @throws IOException
 */
private Document readDocument(File xmlFile)
        throws DocumentException, MalformedURLException, UnsupportedEncodingException, IOException {
    SAXReader xmlReader = new SAXReader();
    xmlReader.setStripWhitespaceText(true);

    org.dom4j.Document dom4jDocument = xmlReader.read(xmlFile);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputFormat format = OutputFormat.createCompactFormat();
    XMLWriter writer = new XMLWriter(baos, format);

    writer.write(dom4jDocument);

    dom4jDocument = DocumentHelper.parseText(baos.toString());

    DOMWriter domWriter = new DOMWriter();
    Document doc = domWriter.write(dom4jDocument);
    return doc;
}

From source file:com.app.util.SettingUtils.java

License:Open Source License

/**
 * /*w ww .  j a  va 2 s .c o  m*/
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File appXmlFile = new ClassPathResource(CommonAttributes.APP_XML_PATH).getFile();
        Document document = new SAXReader().read(appXmlFile);
        List<Element> elements = document.selectNodes("/app/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(appXmlFile);
            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.arc.mw.util.StateSaver.java

License:Open Source License

/**
 * Save the state of the root object into the given file.
 * @param out ascii stream to write to./*from   w  w w  .  j  a va2  s. c o m*/
 * @param object object whose state is to be saved.
 * @exception IOException error occurred in writing file.
 */
public static void saveState(Writer out, IXMLSavable object) throws IOException {
    DocumentFactory factory = DocumentFactory.getInstance();
    Document document = factory.createDocument();
    Element e = factory.createElement("root");
    object.saveState(e);
    document.setRootElement(e);
    XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
    writer.write(document);
}

From source file:com.arthurh.xmergel.Xmergel.java

License:Open Source License

public void combine(List<Path> files)
        throws IOException, SAXException, TransformerException, DocumentException {
    Document finalDocument = this.saxReader.read(files.get(0).toFile());
    for (Path file : files) {
        finalDocument = this.xdtTransformer.transform(finalDocument, this.saxReader.read(file.toFile()));
    }/*from  w  ww  .j  ava  2s.  c  o m*/
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter xmlWriter = new XMLWriter(new FileWriter(this.resultFile.toFile()), format);
    xmlWriter.write(finalDocument);
    xmlWriter.close();
}

From source file:com.augmentum.common.util.XMLFormatter.java

License:Open Source License

public static String toString(Branch branch, String indent, boolean expandEmptyElements) throws IOException {

    ByteArrayMaker bam = new ByteArrayMaker();

    OutputFormat format = OutputFormat.createPrettyPrint();

    format.setExpandEmptyElements(expandEmptyElements);
    format.setIndent(indent);//from ww  w.  ja va2s .  c o m
    format.setLineSeparator("\n");

    XMLWriter writer = new XMLWriter(bam, format);

    writer.write(branch);

    String content = bam.toString(StringPool.UTF8);

    // LEP-4257

    //content = StringUtil.replace(content, "\n\n\n", "\n\n");

    if (content.endsWith("\n\n")) {
        content = content.substring(0, content.length() - 2);
    }

    if (content.endsWith("\n")) {
        content = content.substring(0, content.length() - 1);
    }

    while (content.indexOf(" \n") != -1) {
        content = StringUtil.replace(content, " \n", "\n");
    }

    return content;
}

From source file:com.beetle.framework.resource.container.ContainerConfig.java

License:Apache License

/**
 * /*from   w  ww.  ja v a  2  s  .  c  o  m*/
 *
 * @param tagname --??
 * @param key     --??
 * @throws Exception
 */
public static void setContainValue(String tagname, String key, String value) throws Exception {
    Document doc = XMLReader.getXmlDoc(sysconfigFileName);
    Node node = doc.selectSingleNode(XMLReader.convertPath(tagname));
    if (node != null) {
        @SuppressWarnings("rawtypes")
        Iterator it = node.selectNodes("item").iterator();
        while (it.hasNext()) {
            Element e = (Element) it.next();
            String id = e.valueOf("@name");
            if (id != null && id.equals(key)) {
                e.addAttribute("value", value);
                break;
            }
        }
    }
    File f = new File(sysconfigFileName);
    if (f.exists()) {
        OutputFormat format = OutputFormat.createPrettyPrint();
        FileOutputStream fos = new FileOutputStream(f);
        XMLWriter writer = new XMLWriter(fos, format);
        writer.write(doc);
        writer.close();
    } else {
        AppLogger.getInstance(ContainerConfig.class).error("??jarxml");
    }
}