Example usage for org.dom4j.io XMLWriter flush

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

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the underlying Writer

Usage

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

License:Apache License

/**
 * XML?? XML???/*www .j  av a 2 s  .c om*/
 *
 * @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.ja v a  2  s.  c  om
 *
 * @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.lafengmaker.tool.util.XMLDataUtil.java

License:Open Source License

/**
 * Save to xml file//  w w w  . j a v a  2 s  .co m
 * @param dom
 * @param sFilePathName
 * @param encode
 * @return
 * @throws CommonException
 */
public static boolean saveXML(Document dom, String sFilePathName, String encode) throws Exception {

    File file = new File(sFilePathName);

    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        FileOutputStream out = new FileOutputStream(file);
        // if(!encode.equals(ENCODE_UTF_8)){
        if (encode != null) {
            format.setEncoding(encode);
        }

        // format.setTrimText(true);
        XMLWriter xmlWriter = new XMLWriter(out, format);
        xmlWriter.write(dom);
        xmlWriter.flush();
        xmlWriter.close();
        return true;
    } catch (Exception e) {
        throw new RuntimeException("XMLDATAUTIL-SAVE_DOCUMENT-001", e);
    }

}

From source file:com.liferay.alloy.tools.transformer.AlloyDocsTransformer.java

License:Open Source License

private void _createXML() {
    ArrayList<Component> components = getComponents();

    Document doc = DocumentFactory.getInstance().createDocument();
    Element root = doc.addElement("components");

    root.addAttribute("short-name", _DEFAULT_TAGLIB_SHORT_NAME);
    root.addAttribute("uri", _DEFAULT_TAGLIB_URI);
    root.addAttribute("tlib-version", _DEFAULT_TAGLIB_VERSION);

    for (Component component : components) {
        Element componentNode = root.addElement("component");

        componentNode.addAttribute("name", component.getName());
        componentNode.addAttribute("module", component.getModule());
        componentNode.addAttribute("package", component.getPackage());
        componentNode.addAttribute("bodyContent", String.valueOf(component.isBodyContent()));
        componentNode.addAttribute("alloyComponent", String.valueOf(component.isAlloyComponent()));

        Element descriptionNode = componentNode.addElement("description");
        descriptionNode.addCDATA(component.getDescription());
        Element attributesNode = componentNode.addElement("attributes");
        Element eventsNode = componentNode.addElement("events");

        for (Attribute attribute : component.getAttributes()) {
            Element attributeNode = attributesNode.addElement("attribute");

            Element defaultValueNode = attributeNode.addElement("defaultValue");
            Element attributeDescriptionNode = attributeNode.addElement("description");
            Element javaScriptTypeNode = attributeNode.addElement("javaScriptType");
            Element nameNode = attributeNode.addElement("name");
            Element readOnlyNode = attributeNode.addElement("readOnly");

            defaultValueNode.setText(attribute.getDefaultValue());
            attributeDescriptionNode.addCDATA(_getAttributeDescription(attribute));
            javaScriptTypeNode.setText(attribute.getJavaScriptType());
            nameNode.setText(attribute.getName());
            readOnlyNode.setText(Boolean.toString(attribute.isReadOnly()));
        }/*from   ww w .  ja  va2 s  . co  m*/

        for (Attribute event : component.getEvents()) {
            Element eventNode = eventsNode.addElement("event");
            Element nameNode = eventNode.addElement("name");
            Element typeNode = eventNode.addElement("type");
            Element elementDescriptionNode = eventNode.addElement("description");

            nameNode.setText(event.getName());
            elementDescriptionNode.addCDATA(_getAttributeDescription(event));
            typeNode.setText(event.getType());
        }
    }

    try {
        File file = new File(_outputXML);

        file.getParentFile().mkdirs();

        FileOutputStream fos = new FileOutputStream(file);

        OutputFormat format = OutputFormat.createPrettyPrint();

        XMLWriter writer = new XMLWriter(fos, format);

        writer.write(doc);
        writer.flush();

        System.out.println("Writing " + _outputXML);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.liferay.alloy.tools.xmlbuilder.XMLBuilder.java

License:Open Source License

private void _createXML() {
    ArrayList<Component> components = getComponents();

    Document doc = DocumentFactory.getInstance().createDocument();
    Element root = doc.addElement("taglibs");

    root.addAttribute("short-name", _DEFAULT_TAGLIB_SHORT_NAME);
    root.addAttribute("uri", _DEFAULT_TAGLIB_URI);
    root.addAttribute("tlib-version", _DEFAULT_TAGLIB_VERSION);

    for (Component component : components) {
        Element componentNode = root.addElement("component");

        componentNode.addAttribute("name", component.getName());
        componentNode.addAttribute("module", component.getModule());
        componentNode.addAttribute("package", component.getPackage());
        componentNode.addAttribute("bodyContent", String.valueOf(component.isBodyContent()));

        componentNode.addAttribute("alloyComponent", String.valueOf(component.isAlloyComponent()));

        Element attributesNode = componentNode.addElement("attributes");
        Element eventsNode = componentNode.addElement("events");

        for (Attribute attribute : component.getAttributes()) {
            Element attributeNode = attributesNode.addElement("attribute");
            Element nameNode = attributeNode.addElement("name");
            Element inputTypeNode = attributeNode.addElement("inputType");
            Element outputTypeNode = attributeNode.addElement("outputType");
            Element defaultValueNode = attributeNode.addElement("defaultValue");

            Element descriptionNode = attributeNode.addElement("description");

            nameNode.setText(attribute.getName());
            inputTypeNode.setText(attribute.getInputType());
            outputTypeNode.setText(attribute.getOutputType());
            defaultValueNode.setText(attribute.getDefaultValue());
            descriptionNode.addCDATA(_getAttributeDescription(attribute));
        }//  w ww .j  a  v a2  s. c o  m

        for (Attribute event : component.getEvents()) {
            Element eventNode = eventsNode.addElement("event");
            Element nameNode = eventNode.addElement("name");
            Element typeNode = eventNode.addElement("type");
            Element descriptionNode = eventNode.addElement("description");

            nameNode.setText(event.getName());
            typeNode.setText(event.getInputType());
            descriptionNode.addCDATA(_getAttributeDescription(event));
        }
    }

    try {
        FileOutputStream fos = new FileOutputStream(_componentXML);

        OutputFormat format = OutputFormat.createPrettyPrint();

        XMLWriter writer = new XMLWriter(fos, format);

        writer.write(doc);
        writer.flush();

        System.out.println("Writing " + _componentXML);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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  v  a  2s . co  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.partycommittee.service.PcLicenseService.java

public PcLicenseVo setLicenseInfo(String enstr, String tips) {
    if (enstr == null || enstr.equals(""))
        return null;
    if (tips == null || tips.equals(""))
        return null;
    PcLicenseVo licensevo = new PcLicenseVo();
    Des des = new Des();
    try {/*from   w w w. j  ava2 s. c om*/

        String xml = des.DecryPwdStr(enstr, "hrpweb30");// String 
        System.out.println("xml ==============" + xml);
        if (xml == "" || xml.equals("")) {
            return null;
        }

        if (xml.indexOf("locknodw") <= 0) {
            return null;
        }

        if (xml.indexOf("locknozb") <= 0) {
            return null;
        }

        if (xml.indexOf("root") <= 0) {
            xml = "<root>" + xml + "</root>";
        }
        System.out.println("xml +++++++++++++++++" + xml);
        licensevo = setLicenseVo(xml, tips);

        SAXReader saxReader = new SAXReader();
        Document document = null;
        File file = new File(
                PcLicenseService.class.getClassLoader().getResource("/").getPath() + "license.xml");

        document = saxReader.read(file);
        Element root = document.getRootElement();

        Element license_element = root.element("license");

        license_element.setText(enstr);

        Element tips_element = root.element("tips");

        tips_element.setText(tips);

        String path = PcLicenseService.class.getClassLoader().getResource("/").getPath();
        OutputFormat format = OutputFormat.createPrettyPrint();
        // ??  
        format.setEncoding("UTF8");
        FileOutputStream output = new FileOutputStream(new File(path + "license.xml"));
        XMLWriter writer = new XMLWriter(output, format);
        writer.write(document);
        writer.flush();
        writer.close();

    } catch (Exception e) {
        //e.printStackTrace();
        return null;
    }

    return licensevo;
}

From source file:com.poka.util.XmlSax.java

public boolean writeToXml(Document doc, String file) {
    try {/*from   ww w. j a va2s.  c o  m*/
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(encodingType);
        XMLWriter writer = new XMLWriter(new FileWriter(file), format);
        writer.write(doc);
        writer.flush();
        writer.close();

    } catch (IOException ex) {
        Logger.getLogger(XmlSax.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }
    return true;
}

From source file:com.poka.util.XmlSax.java

public int updateOrAddMachineInfo(MachinesCfg cfg) {
    Document doc = load(bankFile, false);
    Element rootElm = doc.getRootElement();
    Element root1Elm = rootElm.element("machines");
    if (root1Elm == null) {
        root1Elm = rootElm.addElement("machines");
    }/*from w  w  w .j  ava  2s .  c  o  m*/
    List nodes = root1Elm.elements("machine");
    boolean flag = false;
    for (Iterator it = nodes.iterator(); it.hasNext();) {
        Element elm = (Element) it.next();
        if (elm.attributeValue("ip").trim().equals(cfg.getIp().trim())
                && elm.attributeValue("type").trim().equals("" + cfg.getType())) {
            elm.setAttributeValue("machineType", cfg.getMachineType().trim());
            elm.setAttributeValue("machineNum", "" + cfg.getMachineNum());
            elm.setAttributeValue("user1", cfg.getUser1().trim());
            elm.setAttributeValue("user2", cfg.getUser2().trim());
            elm.setAttributeValue("type", "" + cfg.getType());
            flag = true;
            break;
        }
    }
    if (!flag) {
        Element tem = root1Elm.addElement("machine");
        tem.addAttribute("machineType", cfg.getMachineType().trim());
        tem.setAttributeValue("machineNum", "" + cfg.getMachineNum());
        tem.addAttribute("ip", cfg.getIp().trim());
        tem.addAttribute("user1", cfg.getUser1().trim());
        tem.addAttribute("user2", cfg.getUser2().trim());
        tem.addAttribute("type", "" + cfg.getType());
    }
    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        // format.setEncoding("UTF-8");
        format.setEncoding(encodingType);
        XMLWriter writer = new XMLWriter(new FileWriter(bankFile), format);
        writer.write(doc);
        writer.flush();
        writer.close();
        if (flag) {//
            return 0;
        } else {//
            return 1;
        }
        //  return true;
    } catch (IOException ex) {
        Logger.getLogger(XmlSax.class.getName()).log(Level.SEVERE, null, ex);
    }
    return -1;
}

From source file:com.safi.workshop.sqlexplorer.history.SQLHistory.java

License:Open Source License

/**
 * Save all the used queries into a file, so that we can reuse them next time.
 *///from  w  w  w  .j av a  2  s  .  c om
public void save() {

    try {
        File file = new File(ApplicationFiles.SQLHISTORY_FILE_NAME_V350);
        Element root = new DefaultElement(HISTORY);
        for (SQLHistoryElement elem : _history)
            root.add(elem.describeAsXml());
        XMLWriter xmlWriter = new XMLWriter(new FileWriter(file), OutputFormat.createPrettyPrint());
        xmlWriter.write(root);
        xmlWriter.flush();
        xmlWriter.close();

        // Get rid of old versions
        new File(ApplicationFiles.SQLHISTORY_FILE_NAME_V300).delete();

    } catch (IOException e) {
        SQLExplorerPlugin.error("Couldn't save sql history.", e);
    }

}