Java-Dom4jHelper工具类

本文涉及的产品
云解析 DNS,旗舰版 1个月
云解析DNS,个人版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Dom4jHelper {
    /**
     *  解析url xml文档
     * @param url
     * @return
     * @throws DocumentException
     */
    public Document parse(URL url) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(url);
        return document;
    }
    /**
     * 遍历解析文档
     * @param document
     */
    public void treeWalk(Document document) {
        treeWalk( document.getRootElement() );
    }
    /**
     * 遍历解析元素
     * @param element
     */
    public void treeWalk(Element element) {
        for ( int i = 0, size = element.nodeCount(); i < size; i++ ) {
            Node node = element.node(i);
            if ( node instanceof Element ) {
                treeWalk( (Element) node );
            }
            else {
                // 处理....
            }
        }
    }

    /** 
     * 解析文件,获得根元素
     * @param xmlPath
     * @param encoding
     * @return
     * @throws Exception
     */
    public static Element parse(String xmlPath,String encoding)throws Exception{
        //文件是否存在
        File file = new File(xmlPath);
        if(!file.exists()){
            throw new Exception("找不到xml文件:"+xmlPath);
        }

        //解析
        SAXReader reader = new SAXReader(false);
        Document doc = reader.read(new FileInputStream(file),encoding);
        Element root = doc.getRootElement();
        return root;
    }

    /**
     * 保存文档
     * @param doc
     * @param xmlPath
     * @param encoding
     * @throws Exception
     */
    public static void save(Document doc,String xmlPath,String encoding)throws Exception{
        OutputFormat format=OutputFormat.createPrettyPrint();
        format.setEncoding(encoding);
        XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(xmlPath),encoding),format);
        writer.write(doc);
        writer.flush();
        writer.close(); 
    }
    /**
     * 修改xml某节点的值
     * @param inputXml 原xml文件
     * @param nodes 要修改的节点
     * @param attributename 属性名称
     * @param value 新值
     * @param outXml 输出文件路径及文件名 如果输出文件为null,则默认为原xml文件
     */
    public static void modifyDocument(File inputXml, String nodes, String attributename, String value, String outXml) {
        try {
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(inputXml);
            List list = document.selectNodes(nodes);
            Iterator iter = list.iterator();
            while (iter.hasNext()) {
                Attribute attribute = (Attribute) iter.next();
                if (attribute.getName().equals(attributename))
                    attribute.setValue(value);
            }
            XMLWriter output;
            if (outXml != null){ //指定输出文件
                output = new XMLWriter(new FileWriter(new File(outXml)));
            }else{ //输出文件为原文件
                output = new XMLWriter(new FileWriter(inputXml));
            }
            output.write(document);
            output.close();
        }

        catch (DocumentException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }   

    /**
     * xml转换为字符串
     * @param doc
     * @param encoding
     * @return
     * @throws Exception
     */
    public static String toString(Document doc,String encoding)throws Exception{
        OutputFormat format=OutputFormat.createPrettyPrint();
        format.setEncoding(encoding);
        ByteArrayOutputStream byteOS=new ByteArrayOutputStream();
        XMLWriter writer = new XMLWriter(new OutputStreamWriter(byteOS,encoding),format);
        writer.write(doc);
        writer.flush();
        writer.close();     
        writer=null;

        return byteOS.toString(encoding);
    }
    /**
     * 字符串转换为Document
     * @param text
     * @return
     * @throws DocumentException
     */
    public static Document str2Document(String text) throws DocumentException{
        Document document = DocumentHelper.parseText(text);
        return document;
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}
目录
相关文章
|
4天前
|
XML Java 数据格式
必知的技术知识:java基础73dom4j修改xml里面的内容(网页知识)
必知的技术知识:java基础73dom4j修改xml里面的内容(网页知识)
10 1
|
2天前
|
设计模式 缓存 算法
编写高效的Java工具类:实用技巧与设计模式
编写高效的Java工具类:实用技巧与设计模式
|
5天前
|
Java
JAVA工具类匹配重复或者连续的字符和符号
JAVA工具类匹配重复或者连续的字符和符号
8 2
|
6天前
|
算法 Java
基于java雪花算法工具类SnowflakeIdUtils-来自chatGPT
基于java雪花算法工具类SnowflakeIdUtils-来自chatGPT
13 3
|
7天前
|
Java easyexcel
java开发excel导入导出工具类基于EasyExcel
java开发excel导入导出工具类基于EasyExcel
15 1
|
8天前
|
Java
java工具类调用service层,mapper层
java工具类调用service层,mapper层
9 1
|
12天前
|
Java 数据库连接
Java的数据库连接工具类的编写
Java的数据库连接工具类的编写
14 1
|
19天前
|
Java 数据安全/隐私保护
JAVA中MD5加密(MD5工具类)
JAVA中MD5加密(MD5工具类)
17 1
|
27天前
|
存储 并行计算 Java
Java8中JUC包同步工具类深度解析(Semaphore,CountDownLatch,CyclicBarrier,Phaser)
Java8中JUC包同步工具类深度解析(Semaphore,CountDownLatch,CyclicBarrier,Phaser)
19 2
|
5天前
|
XML 安全 Java
一篇文章讲明白JAVA常用的工具类
一篇文章讲明白JAVA常用的工具类