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.beetle.framework.util.file.XMLReader.java

License:Apache License

public static void setTagContent(String xmlFileName, String TagPath, String value) {
    synchronized (writeLock) {
        SAXReader reader = new SAXReader();
        XMLWriter writer = null;
        try {/*from  w  w w.  j a v  a  2 s  . co  m*/
            Document doc = reader.read(new File(xmlFileName));
            Node node = doc.selectSingleNode(convertPath(TagPath));
            if (node != null) {
                node.setText(value);
            }
            writer = new XMLWriter(new FileWriter(xmlFileName));
            writer.write(doc);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            reader = null;
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}

From source file:com.beetle.framework.util.file.XMLReader.java

License:Apache License

public static void setProperties(String xmlFileName, String itemPath, String ElementName, String keyName,
        String valueName, String key, String value) {
    // ? ??????//from w  w w. ja  va 2 s . com
    // ????
    synchronized (writeLock) {
        SAXReader reader = new SAXReader();
        XMLWriter writer = null;
        try {
            Document doc = reader.read(new File(xmlFileName));
            Node node = doc.selectSingleNode(convertPath(itemPath));
            if (node != null) {
                Iterator<?> it = node.selectNodes(ElementName).iterator();
                while (it.hasNext()) {
                    Element e = (Element) it.next();
                    if (e.attributeValue(keyName).equals(key)) {
                        e.addAttribute(valueName, value);
                        break;
                    }
                }
            }
            writer = new XMLWriter(new FileWriter(xmlFileName));
            writer.write(doc);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            reader = null;
        }
    }
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * XMLDocumentjava.io.Writer?//from  ww  w .ja  v a 2  s .co m
 *
 *
 *
 * ??Schema
 *
 *
 *
 * @param document
 *            XML
 * @param outWriter
 *            
 *
 *
 *
 * @param encoding
 *            ?
 * @throws XMLDocException
 *             
 *
 *
 * @throws BaseException
 *
 */
public static void toXML(Document document, java.io.Writer outWriter, String encoding) throws BaseException {
    //
    OutputFormat outformat = OutputFormat.createPrettyPrint();
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    // ?
    outformat.setEncoding(encoding);
    XMLWriter xmlWriter = null;
    try {
        xmlWriter = new XMLWriter(outWriter, outformat);
        xmlWriter.write(document);
        xmlWriter.flush();
    } catch (IOException ex) {
        throw new BaseException("UTIL-0001", ex);
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (IOException ex) {
            }
        }
    }
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * XMLDocumentjava.io.Writer?//w  ww  .  j  a v a  2  s. c  o  m
 *
 *
 *
 * ??Schema
 *
 *
 *
 * @param document
 *            XML
 * @param outStream
 *            
 *
 *
 *
 * @param encoding
 *            ?
 * @throws XMLDocException
 *             
 *
 *
 * @throws BaseException
 *
 */
public static void toXML(Document document, java.io.OutputStream outStream, String encoding)
        throws BaseException {
    //
    OutputFormat outformat = new OutputFormat();
    outformat.setIndentSize(0);
    outformat.setNewlines(true);
    outformat.setTrimText(true);

    // OutputFormat outformat = OutputFormat.createPrettyPrint();
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    // ?
    outformat.setEncoding(encoding);
    XMLWriter xmlWriter = null;
    try {
        xmlWriter = new XMLWriter(new OutputStreamWriter(outStream), outformat);
        xmlWriter.write(document);
        xmlWriter.flush();
    } catch (IOException ex) {
        throw new BaseException("UTIL-0001", ex);
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (IOException ex) {
            }
        }
    }
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

public static void element2XML(Element element, java.io.OutputStream outStream, String encoding)
        throws BaseException {
    ///*  w  w w .j a v  a 2  s .  c  o  m*/
    OutputFormat outformat = new OutputFormat();
    outformat.setIndentSize(0);
    outformat.setNewlines(false);
    outformat.setTrimText(true);

    // OutputFormat outformat = OutputFormat.createPrettyPrint();
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    // ?
    outformat.setEncoding(encoding);
    XMLWriter xmlWriter = null;
    try {
        xmlWriter = new XMLWriter(new OutputStreamWriter(outStream), outformat);
        xmlWriter.write(element);
        xmlWriter.flush();
    } catch (IOException ex) {
        throw new BaseException("UTIL-0001", ex);
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (IOException ex) {
            }
        }
    }
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * This method will generate XML file in a StringBuffer based on the given
 * Dom4j object.//w w  w  .  j  ava 2 s .  c o m
 * 
 * @param xmlObj
 *            Object
 * @param encoding
 *            String
 * @throws IOException
 * @return StringBuffer
 * @throws BaseException
 */
public static StringBuffer generateXMLStringBuffer(Object xmlObj, String encoding) throws BaseException {
    StringWriter writer = new StringWriter();
    OutputFormat outformat = OutputFormat.createPrettyPrint();

    // ?
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    outformat.setEncoding(encoding);

    // dom4j ?OBJECT
    XMLWriter xmlWriter = null;
    xmlWriter = new XMLWriter(writer, outformat);

    try {
        xmlWriter.write(xmlObj);
        xmlWriter.flush();
    } catch (Exception ex) {
        throw new BaseException("UTIL-0002", ex);
    }

    return writer.getBuffer();
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * XML?/* w  w  w  .  ja v  a2 s  .  c  om*/
 * 
 * @param xmlObj
 * @param encoding
 * @param filename
 * @return
 * @throws BaseException
 */
public static boolean generateXMLFile(Object xmlObj, String encoding, String filename) throws BaseException {
    FileWriter writer = null;
    OutputFormat outformat = OutputFormat.createPrettyPrint();

    // ?
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    outformat.setEncoding(encoding);

    // dom4j ?OBJECT
    try {
        writer = new FileWriter(filename);
        XMLWriter xmlWriter = null;
        xmlWriter = new XMLWriter(writer, outformat);
        xmlWriter.write(xmlObj);
        xmlWriter.flush();
    } catch (Exception ex) {
        throw new BaseException("UTIL-0004", ex);
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (IOException e) {
            }
    }

    return true;
}

From source file:com.brick.util.nciic.NciicUtil.java

/**
 * ?XML// ww w .  j a  v  a 2  s.c  om
 * @param text
 * @return
 * @throws Exception
 */
public static List<NciicEntity> readResult(String text) throws Exception {
    List<NciicEntity> resultList = new ArrayList<NciicEntity>();
    NciicEntity result = null;
    Document d;
    XMLWriter writer = null;
    try {
        //SAXReader reader = new SAXReader();
        //d = reader.read(new File("d:/test/testxml.xml"));
        d = DocumentHelper.parseText(text);
        String dateStr = DateUtil.dateToString(new Date(), "[yyyy-MM-dd][HH-mm]");
        File xmlPath = new File(XML_PATH);
        if (!xmlPath.exists()) {
            xmlPath.mkdirs();
        }
        File xpPath = new File(XP_PATH);
        if (!xpPath.exists()) {
            xpPath.mkdirs();
        }
        writer = new XMLWriter(new FileOutputStream(new File(xmlPath, dateStr + ".xml")));
        writer.write(d);
        writer.flush();
        writer.close();
        writer = null;

        Element root = d.getRootElement();
        List<Element> allResult = root.elements("ROW");
        Element input = null;
        List<Element> output = null;
        String result_msg = null;
        for (Element element : allResult) {
            result = new NciicEntity();
            result_msg = null;
            input = element.element("INPUT");
            result.setGmsfhm(input.element("gmsfhm").getText());
            result.setXm(input.element("xm").getText());
            output = element.element("OUTPUT").elements("ITEM");
            for (Element out_element : output) {
                if (out_element.element("result_gmsfhm") != null) {
                    result.setResult_gmsfhm(out_element.element("result_gmsfhm").getText());
                }
                if (out_element.element("result_xm") != null) {
                    result.setResult_xm(out_element.element("result_xm").getText());
                }
                if (out_element.element("errormesage") != null) {
                    result.setError_msg(out_element.element("errormesage").getText());
                }
                if (out_element.element("errormesagecol") != null) {
                    result.setError_msg_col(out_element.element("errormesagecol").getText());
                }
                if (out_element.element("xp") != null) {
                    result.setXp(out_element.element("xp").getText());
                }
                if (!StringUtils.isEmpty(result.getXp())) {
                    try {
                        File f = new File(xpPath, result.getGmsfhm() + "-" + result.getXm() + ".jpg");
                        BufferedImage img = ImageIO.read(
                                new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(result.getXp())));
                        ImageIO.write(img, "jpg", f);
                        result.setXp_file(f.getPath());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            if ("".equals(result.getResult_gmsfhm()) && "".equals(result.getResult_xm())) {
                result_msg = "";
            } else {
                if ("?".equals(result.getResult_gmsfhm())) {
                    result_msg = "???";
                } else if ("?".equals(result.getResult_xm())) {
                    result_msg = "???";
                } else if (!StringUtils.isEmpty(result.getError_msg())) {
                    result_msg = result.getError_msg();
                    if (!StringUtils.isEmpty(result.getError_msg_col())) {
                        result_msg += "(" + result.getError_msg_col() + ")";
                    }
                }
            }
            result.setResult_msg(result_msg);
            resultList.add(result);
        }
        return resultList;
    } catch (Exception e) {
        throw e;
    } finally {
        if (writer != null) {
            writer.flush();
            writer.close();
            writer = null;
        }
    }
}

From source file:com.bsoft.baseframe.baseframe_utils.beanUtils.FileUtils.java

License:Open Source License

/**
 * ?XML?/*  w  ww  .j  a  va2 s .  com*/
 * @param document 
 * @param file
 * @throws IOException
 */
public static boolean wrieteXML2Doc(Document document, File file) {
    boolean isCreate = false;
    try {
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();// 
        }
        if (!file.exists()) {
            isCreate = true;
            file.createNewFile();// java testData.java
        } else {
            isCreate = false;
        }
        //?XML??
        if (isCreate) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8"); //
            //document
            XMLWriter writer = new XMLWriter(new FileWriter(file), format);
            writer.write(document);
            writer.close();
        }
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:com.bstek.dorado.idesupport.output.RuleSetOutputter.java

License:Open Source License

protected void outputPackageInfos(XMLWriter xmlWriter, RuleTemplateManager ruleTemplateManager,
        OutputContext context) throws Exception {
    Map<String, PackageInfo> finalPackageInfos = new LinkedHashMap<String, PackageInfo>(
            PackageManager.getPackageInfoMap());

    Collection<PackageInfo> packageInfos = ruleTemplateManager.getPackageInfos();
    if (packageInfos != null) {
        for (PackageInfo packageInfo : packageInfos) {
            finalPackageInfos.put(packageInfo.getName(), packageInfo);
        }/*www .  j  ava2 s  . co m*/
    }

    Element packageInfosElement = DocumentHelper.createElement("PackageInfos");
    xmlWriter.writeOpen(packageInfosElement);
    for (PackageInfo packageInfo : finalPackageInfos.values()) {
        Element packageInfoElement = DocumentHelper.createElement("PackageInfo");
        setElementAttributes(packageInfoElement, packageInfo, "name,version");
        xmlWriter.write(packageInfoElement);
    }
    xmlWriter.writeClose(packageInfosElement);
}