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.googlecode.fascinator.common.sax.SafeSAXReader.java

License:Open Source License

/**
 * Convert node to stream//from  w ww . ja v  a  2  s. c  om
 * 
 * @param outDoc Node to be converted
 * @param outStream output stream of the converted node
 * @throws IOException if the conversion fail
 */
public void docToStream(Node outDoc, OutputStream outStream) throws IOException {
    OutputFormat opf = new OutputFormat("", false, "UTF-8");
    opf.setSuppressDeclaration(true);
    opf.setExpandEmptyElements(true);
    XMLWriter writer = new XMLWriter(outStream, opf);
    writer.setEscapeText(false);
    writer.write(outDoc);
    writer.close();
}

From source file:com.googlecode.starflow.engine.xml.XmlFormat.java

License:Apache License

/**
 * dom4j'pretty'??dom.// ww w. j av a  2s .  c o  m
 * 
 * @param doc
 *            ??dom.
 * @param encoding
 *            ??.
 * @return ??XML.null,???.
 */
public static String getPrettyString(Document doc, String encoding) {
    StringWriter writer = new StringWriter();
    OutputFormat format = OutputFormat.createPrettyPrint();
    if (encoding == null || "".equals(encoding.trim())) {
        encoding = "GBK";
    }
    format.setEncoding(encoding);
    XMLWriter xmlwriter = new XMLWriter(writer, format);

    try {
        xmlwriter.write(doc);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return writer.toString();
}

From source file:com.gote.pojo.Tournament.java

License:Apache License

/**
 * Transform Tournament in a formatted XML
 * //from w  w w .  j a v  a 2 s .c  om
 * @return XML to write as a String
 */
public String toXML() {
    // Create document
    Document doc = DocumentHelper.createDocument();

    // Create tournament element
    Element root = doc.addElement(TournamentGOTEUtil.TAG_TOURNAMENT);
    // Add attributes
    root.addAttribute(TournamentGOTEUtil.ATT_TOURNAMENT_NAME, getTitle());
    root.addAttribute(TournamentGOTEUtil.ATT_TOURNAMENT_SERVER, getServerType());
    root.addAttribute(TournamentGOTEUtil.ATT_TOURNAMENT_DATESTART, getStartDate().toString());
    root.addAttribute(TournamentGOTEUtil.ATT_TOURNAMENT_DATEEND, getEndDate().toString());

    // Add rules
    getTournamentRules().toXML(root);

    // Add players
    Element players = root.addElement(TournamentGOTEUtil.TAG_PLAYERS);
    for (Player player : getParticipantsList()) {
        player.toXML(players);
    }
    // Add rounds
    Element rounds = root.addElement(TournamentGOTEUtil.TAG_ROUNDS);
    for (Round round : getRounds()) {
        round.toXML(rounds);
    }

    StringWriter out = new StringWriter(1024);
    XMLWriter writer;
    try {
        writer = new XMLWriter(OutputFormat.createPrettyPrint());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
    writer.setWriter(out);
    try {
        writer.write(doc);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Return the friendly XML
    return out.toString();
}

From source file:com.haulmont.bali.util.Dom4j.java

License:Apache License

public static void writeDocument(Document doc, boolean prettyPrint, Writer writer) {
    XMLWriter xmlWriter;
    try {/*  w  w  w. j a va  2s  . c o  m*/
        if (prettyPrint) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            xmlWriter = new XMLWriter(writer, format);
        } else {
            xmlWriter = new XMLWriter(writer);
        }
        xmlWriter.write(doc);
    } catch (IOException e) {
        throw new RuntimeException("Unable to write XML", e);
    }
}

From source file:com.haulmont.bali.util.Dom4j.java

License:Apache License

public static void writeDocument(Document doc, boolean prettyPrint, OutputStream stream) {
    XMLWriter xmlWriter;
    try {//from w  ww  .java2 s . c o m
        if (prettyPrint) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            xmlWriter = new XMLWriter(stream, format);
        } else {
            xmlWriter = new XMLWriter(stream);
        }
        xmlWriter.write(doc);
    } catch (IOException e) {
        throw new RuntimeException("Unable to write XML", e);
    }
}

From source file:com.haulmont.cuba.restapi.XMLConverter2.java

License:Apache License

protected String documentToString(Document document) {
    try {//  www.ja v  a 2 s  . co m
        OutputFormat format = OutputFormat.createPrettyPrint();
        StringWriter sw = new StringWriter();
        XMLWriter writer = new XMLWriter(sw, format);
        writer.write(document);
        return sw.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java

License:Apache License

/**
 * XML?? XML???//from  w w w .  ja v a  2 s  .  co  m
 *
 * @param document
 *            ?
 * @param file
 *            ?XML
 */
public void writeXml(Document document, File file) {
    try {
        //?
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter output = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"), format);
        output.write(document);
        output.flush();
        output.close();
    } catch (IOException e) {
        log.info(e.getMessage());
    }

}

From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java

License:Apache License

/**
 * XML?? ?//w  w  w .  j  ava2s.c o m
 *
 * @param document
 *            ?
 * @param file
 *            ?XML
 */
public void writeXml(Document document) {
    try {
        //?
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter output = new XMLWriter(System.out, format);
        //XMLWriter output = new XMLWriter(new FileWriter(file));
        output.write(document);
        output.flush();
        output.close();
    } catch (IOException e) {
        log.info(e.getMessage());
    }

}

From source file:com.intuit.tank.script.ScriptEditor.java

License:Open Source License

public String getPrettyString(String s, String mimetype) {
    if (StringUtils.isNotBlank(s)) {
        if (StringUtils.containsIgnoreCase(mimetype, "json")) {
            try {
                JsonParser parser = new JsonParser();
                Gson gson = new GsonBuilder().setPrettyPrinting().create();
                JsonElement el = parser.parse(s);
                s = gson.toJson(el); // done
            } catch (JsonSyntaxException e) {
                LOG.warn("Cannot format json string: " + e);
            }/* w  ww  .  j  av  a  2 s .c  om*/
        } else if (StringUtils.containsIgnoreCase(mimetype, "xml")) {
            try {
                StringWriter sw;
                final OutputFormat format = OutputFormat.createPrettyPrint();
                final org.dom4j.Document document = DocumentHelper.parseText(s);
                sw = new StringWriter();
                final XMLWriter writer = new XMLWriter(sw, format);
                writer.write(document);
                s = sw.toString();
            } catch (Exception e) {
                LOG.warn("Cannot format xml string: " + e);
            }
        }
        s = s.trim();
    }
    return s;
}

From source file:com.iterzp.momo.utils.SettingUtils.java

License:Open Source License

/**
 * /*from w  ww .  j a va  2  s  . c o  m*/
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File shopxxXmlFile = new ClassPathResource(CommonAttributes.MOMO_XML_PATH).getFile();
        Document document = new SAXReader().read(shopxxXmlFile);
        List<Element> elements = document.selectNodes("/momo/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();
    }
}