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.maomao.framework.utils.XmlUtils.java

License:Apache License

public void saveXml2File(Document doc, File file) {
    XMLWriter writer = null;
    try {/* w ww  .j  a v a2s .co  m*/
        FileWriter fw = new FileWriter(file);
        writer = new XMLWriter(fw);
        writer.write(doc);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != writer)
            try {
                writer.close();
            } catch (Exception ie) {
            }
    }
}

From source file:com.mor.blogengine.xml.io.XmlDataSourceProvider.java

License:Open Source License

/**
 * writes in a file/*from  w ww .j a v  a  2 s  .c o  m*/
 *
 * @param document DOM model to write in
 * @param pOutputFile output file
 * @throws java.io.IOException
 * @param pDocument
 */
boolean write(Document pDocument) throws MissingPropertyException, IncorrectPropertyValueException {
    boolean ret = false;
    try {
        OutputFormat format = new OutputFormat();
        format.setEncoding(getFileEncoding());
        XMLWriter writer;
        writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(xml.getFile()),
                Charset.forName(getFileEncoding())));

        writer.write(pDocument);
        writer.close();
        ret = true;

    } catch (IOException ex) {
        trace("Error saving file..." + ex);

    }
    return ret;

}

From source file:com.ms.commons.test.treedb.Objects2XmlFileUtil.java

License:Open Source License

private static String prettyPrint(final String xml) {

    if (StringUtils.isBlank(xml)) {
        throw new RuntimeException("xml was null or blank in prettyPrint()");
    }//from   w  ww  .  j av  a2  s  . com

    final StringWriter sw;

    try {
        final OutputFormat format = OutputFormat.createPrettyPrint();
        final org.dom4j.Document document = DocumentHelper.parseText(xml);
        sw = new StringWriter();
        final XMLWriter writer = new XMLWriter(sw, format);
        writer.write(document);
    } catch (Exception e) {
        throw new RuntimeException("Error pretty printing xml:\n" + xml, e);
    }
    return sw.toString();
}

From source file:com.nokia.ant.Database.java

License:Open Source License

public void createXMLFile(File outputFile) {
    try {//from   w  w  w  . j a v a  2 s .  co m
        Document outDoc = createDOM();
        OutputStream outStream = System.out;
        if (outputFile != null) {
            outStream = new FileOutputStream(outputFile);
        }
        XMLWriter out = new XMLWriter(outStream, OutputFormat.createPrettyPrint());
        out.write(outDoc);
    } catch (Exception e) {
        throw new BuildException(e.getMessage());
    }
}

From source file:com.nokia.ant.ModelPropertiesParser.java

License:Open Source License

/**
 * Writes Document object as xml file/* w w w. j  a v a2  s. c  om*/
 */
private void writeXMLFile() {
    try {
        if (outputPath != null) {
            XMLWriter out = new XMLWriter(new FileOutputStream(outputPath), OutputFormat.createPrettyPrint());
            out.write(doc);
        }
    } catch (FileNotFoundException e) {
        // We are Ignoring the errors as no need to fail the build.
        log.debug("Not able to write into XML Document " + e.getMessage());
    } catch (UnsupportedEncodingException e) {
        // We are Ignoring the errors as no need to fail the build.
        log.debug("Not able to write into XML Document " + e.getMessage());
    } catch (IOException e) {
        // We are Ignoring the errors as no need to fail the build.
        log.debug("Not able to write into XML Document " + e.getMessage());
    }
}

From source file:com.nokia.helium.core.ant.filters.PrettyPrintXmlFilter.java

License:Open Source License

/**
 * Filter the input string./* ww w  .j a v  a 2 s  . c  o  m*/
 * 
 * @param string
 *            the string to filter
 * @return the modified string
 */
public String filter(String token) {
    String output = token;
    XMLWriter writer = null;
    if (token.length() > 0) {
        try {
            Document doc = DocumentHelper.parseText(token);
            StringWriter out = new StringWriter();
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setIndentSize(4);
            writer = new XMLWriter(out, format);
            writer.write(doc);

            output = out.toString();
        } catch (org.dom4j.DocumentException exc) {
            throw new BuildException(exc.getMessage(), exc);
        } catch (IOException exc) {
            throw new BuildException(exc.getMessage(), exc);
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException exc) {
                throw new BuildException(exc.getMessage(), exc);
            }
        }
    }
    return output;
}

From source file:com.nokia.helium.diamonds.XMLMerger.java

License:Open Source License

/**
 * Write the XML content back the file.//from w w w . ja va  2  s  .  c  o m
 * @throws XMLMergerException
 */
protected void write() throws XMLMergerException {
    try {
        FileOutputStream fos = new FileOutputStream(outputFile);
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(fos, format);
        writer.write(doc);
        writer.flush();
    } catch (FileNotFoundException e) {
        throw new XMLMergerException(e.getMessage());
    } catch (UnsupportedEncodingException e) {
        throw new XMLMergerException(e.getMessage());
    } catch (IOException e) {
        throw new XMLMergerException(e.getMessage());
    }
}

From source file:com.nokia.helium.environment.EnvironmentXMLWriter.java

License:Open Source License

/**
 * Writes an environment definition in XML to output.
 * @param environment Enviroment definition.
 * @throws IOException If I/O error occurs.
 *///from  w w w  .jav a 2  s . co m
public void write(Environment environment) throws IOException {
    doc = DocumentFactory.getInstance().createDocument();
    doc.addElement("environment");
    List<Executable> executables = environment.getExecutables();
    for (Iterator<Executable> iterator = executables.iterator(); iterator.hasNext();) {
        Executable executable = (Executable) iterator.next();
        write(executable);
    }
    XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
    writer.write(doc);
    writer.close();
}

From source file:com.nokia.helium.internaldata.ant.listener.XMLRenderer.java

License:Open Source License

/**
 * Rendering the build node into XML string. 
 *///w  w  w  .  jav a2  s .  c o  m
public String toString() {
    // Creating the XML document
    Document document = DocumentHelper.createDocument();
    Element statistics = document.addElement("statistics");
    statistics.addAttribute("version", "1.1");

    // Creating the document content.
    insertDatabase(statistics);
    createTargets(statistics);
    createAsserts(statistics);
    createExecutionTree(statistics);
    createProperties(statistics);
    try {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        XMLWriter out = new XMLWriter(output, OutputFormat.createPrettyPrint());
        out.write(document);
        return output.toString();
    } catch (UnsupportedEncodingException exc) {
        return document.asXML();
    } catch (IOException exc) {
        return document.asXML();
    }
}

From source file:com.npower.dm.hibernate.management.ModelManagementBeanImpl.java

License:Open Source License

/**
 * //  w w w .j  a va  2  s. com
 * <pre>
 * Export the TAC form database by Model.
 * &lt;pre&gt;
 * 
 * @param model
 * @return
 * @throws DMException
 * 
 */
public void exportModelTAC(Model model, String outFile) throws DMException {

    Document document = org.dom4j.DocumentHelper.createDocument();
    Element rootElement = document.addElement("Manufacturers");
    Element manElement = rootElement.addElement("Manufacturer");
    Element manNameElement = manElement.addElement("Name");
    manNameElement.setText(model.getManufacturer().getName());
    Element manexterIDElement = manElement.addElement("ExternalID");
    manexterIDElement.setText(model.getManufacturer().getExternalId());
    Element modelElement = manElement.addElement("Model");
    Element modelNameElement = modelElement.addElement("Name");
    modelNameElement.setText(model.getName());
    Element modelexterIDElement = modelElement.addElement("ExternalID");
    modelexterIDElement.setText(model.getManufacturerModelId());
    Element TACSElement = modelElement.addElement("TACS");

    Set<String> tacSet = model.getModelTAC();
    for (String tac : tacSet) {
        Element TACElement = TACSElement.addElement("TAC");
        TACElement.setText(tac);
    }

    try {
        XMLWriter writer = new XMLWriter(new FileWriter(new File(outFile)));
        writer.write(document);
        writer.close();
        this.formatXMLFile(outFile);
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }

}