java Xml/Dom工具类 XMLUtil4DOM

public class XMLUtil4DOM {
    public static Document file2Document(String filePath) {
        Document doc = null;
        FileInputStream fis = null;
        try {
            SAXReader saxReader = new SAXReader();
            saxReader.setEncoding("UTF-8");
            File f = new File(filePath);
            if (f.exists()) {
                fis = new FileInputStream(f);
                doc = saxReader.read(fis);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return doc;
    }

    public static Document file2Document(File file) {
        Document doc = null;
        FileInputStream fis = null;
        try {
            SAXReader saxReader = new SAXReader();
            saxReader.setEncoding("UTF-8");
            if (file.exists()) {
                fis = new FileInputStream(file);
                doc = saxReader.read(fis);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return doc;
    }

    public static boolean document2File(Document doc, String filePath) {
        boolean returnCode = true;
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        format.setOmitEncoding(false);
        XMLWriter writer = null;
        try {
            File f = new File(filePath);
            if (!f.exists())
                f.createNewFile();
            writer = new XMLWriter(new FileOutputStream(f), format);
            writer.write(doc);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e1) {
                e1.printStackTrace();
                returnCode = false;
            }
        }
        return returnCode;
    }

    public static Document inputStream2Document(InputStream inputStream) {
        Document doc = null;
        if (inputStream != null) {
            try {
                SAXReader saxReader = new SAXReader();
                saxReader.setEncoding("UTF-8");
                doc = saxReader.read(inputStream);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return doc;
    }

    public static Document string2Document(String sXML) {
        Document doc = null;
        if (sXML != null) {
            try {
                doc = DocumentHelper.parseText(sXML);
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        }
        return doc;
    }

    public static void output(Document doc, OutputStream output) {
        if (doc != null) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            XMLWriter writer = null;
            try {
                writer = new XMLWriter(output, format);
                writer.write(doc);
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    writer.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public static void outputWithException(Document doc, OutputStream output) throws Exception {
        if (doc != null) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            XMLWriter writer = null;
            writer = new XMLWriter(output, format);
            writer.write(doc);
            writer.close();
        }
    }

    public static void output(Element ele, OutputStream output) {
        if (ele != null) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            XMLWriter writer = null;
            try {
                writer = new XMLWriter(output, format);
                writer.write(ele);
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    writer.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public static void string2OutputStream(String xml, OutputStream out) {
        try {
            output(string2Document(xml), out);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void document2OutputStream(Document doc, OutputStream out) {
        output(doc, out);
    }

    public static String document2String(Document doc) {
        if (doc != null) {
            return doc.asXML();
        }
        return null;
    }

    public static String element2String(Element ele) {
        if (ele != null) {
            return ele.asXML();
        }
        return null;
    }

    public static void setAttribute(Element ele, String key, String value) {
        if ((ele == null) || (key == null) || (value == null))
            return;
        if (ele.attribute(key) == null)
            ele.addAttribute(key, value);
        else
            ele.attribute(key).setText(value);
    }

    public static void setCDATA(Element ele, String cdata) {
        if (ele == null)
            return;
        ele.clearContent();
        ele.add(new DOMCDATA(cdata));
    }

    public static void getEmptyDocXml(OutputStream output, Boolean isLogin) {
        if (output != null) {
            Document doc = DocumentHelper.createDocument();
            Element rootEle = doc.addElement("root");
            if (Boolean.TRUE.equals(isLogin))
                rootEle.addAttribute("isLogin", "1");
            else
                rootEle.addAttribute("isLogin", "0");
            document2OutputStream(doc, output);
        }
    }

    public static void getEmptyMainData(OutputStream output, String errorMessage, boolean flag) {
        Document doc = DocumentHelper.createDocument();
        Element rootEle = doc.addElement("main_root");
        rootEle.addAttribute("flag", flag + "");
        if ((errorMessage != null) && (errorMessage.length() > 0)) {
            Element errorMsg = rootEle.addElement("errorMessage");
            errorMsg.setText(errorMessage);
            document2OutputStream(doc, output);
        }
    }

    public static void getStateXML(OutputStream output, boolean flag, String message) {
        Document doc = DocumentHelper.createDocument();
        Element rootEle = doc.addElement("root");
        if ((!flag) && (message != null) && (message.length() > 0)) {
            Element errorMsg = rootEle.addElement("errorMessage");
            errorMsg.addCDATA(message);
        }
        if (flag)
            rootEle.addAttribute("flag", "1");
        else
            rootEle.addAttribute("flag", "0");
        document2OutputStream(doc, output);
    }

    public static void getInfoDocXml(OutputStream output, String infoMessage, boolean flag) {
        Document doc = DocumentHelper.createDocument();
        Element rootEle = doc.addElement("root");
        if (flag == true)
            rootEle.addAttribute("flag", "1");
        else
            rootEle.addAttribute("flag", "0");
        if ((infoMessage != null) && (infoMessage.length() > 0)) {
            Element errorMsg = rootEle.addElement("InfoMessage");
            errorMsg.setText(infoMessage);
            document2OutputStream(doc, output);
        }
    }

    public static List getChildren(Object ele, String childPath) {
        if ((ele == null) || (childPath == null))
            return null;
        XPath path = new DefaultXPath(childPath);
        return path.selectNodes(ele);
    }

    public static Object getChild(Object element, String childPath) {
        if ((element == null) || (childPath == null))
            return null;
        XPath path = new DefaultXPath(childPath);
        return path.selectSingleNode(element);
    }

    public static void getErrorDocXml(OutputStream output, int errorCode) {
        getErrorDocXml(output, errorCode);
    }

    public static void getErrorDocXml(OutputStream output, String errorMessage) {
        Document doc = DocumentHelper.createDocument();
        Element rootEle = doc.addElement("root");
        rootEle.addAttribute("flag", "0");
        if ((errorMessage != null) && (errorMessage.length() > 0)) {
            Element errorMsg = rootEle.addElement("errorMessage");
            errorMsg.setText(errorMessage);
            document2OutputStream(doc, output);
        }
    }

    public static void getInfoDocXml(OutputStream output, String infoMessage) {
        Document doc = DocumentHelper.createDocument();
        Element rootEle = doc.addElement("root");
        if ((infoMessage != null) && (infoMessage.length() > 0)) {
            Element errorMsg = rootEle.addElement("InfoMessage");
            errorMsg.setText(infoMessage);
            document2OutputStream(doc, output);
        }
    }

    public static Document file2Dom(InputStream is) {
        Document dom = null;
        BufferedInputStream bis = null;
        try {
            SAXReader sax = new SAXReader();
            sax.setEncoding("UTF-8");
            bis = new BufferedInputStream(is);
            dom = sax.read(bis);
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return dom;
    }
}

package com.xj.hhjk.common.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.dom.DOMCDATA;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.xpath.DefaultXPath;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长青风

赏一块,发大财!赏两块,惹人爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值