欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java DOM4J 读取XML实例代码

程序员文章站 2023-12-20 17:43:28
下面展示一篇我自己写的一个xml读取测试 复制代码 代码如下:import java.util.iterator;import java.io.bufferedreade...

下面展示一篇我自己写的一个xml读取测试

复制代码 代码如下:

import java.util.iterator;
import java.io.bufferedreader;
import java.io.file;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.net.malformedurlexception;
import org.dom4j.*;
import org.dom4j.io.saxreader;

public class xmlread {

    static stringbuilder sbuilder = new stringbuilder();

    public static void main(string[] args) throws ioexception {

        bufferedreader breader = new bufferedreader(new inputstreamreader(
                system.in));
        string path = null;
        system.out.println("请输入xml文件的绝对路径以及文件名:\n");
        path = breader.readline();

        sbuilder.append("开始输出xml文件内容\n");

        document document = null;
        try {
            document = read(path);
            sbuilder.append(path + "\n");
        } catch (malformedurlexception e) {
            e.printstacktrace();
        } catch (documentexception e) {
            e.printstacktrace();
        }

        element root = getrootelement(document);
        if (root == null) {
            system.out.print("没有获取到root节点");
            return;
        }
        // 获取xml文档的编码格式
        string encstring = document.getxmlencoding();
        sbuilder.append("<?xml version=\"1.0\" encoding=\"" + encstring
                + "\">\n");
        sbuilder.append(elementtext(root, atttext(root), 0));

        system.out.println(getiterator(root, 0) + "</" + root.getname() + ">");

    }

    /**
     * 递归节点
     *
     * @description
     * @param element
     * @param lvl
     *            层级
     * @return
     */
    private static string getiterator(element element, int lvl) {

        lvl += 1;

        for (iterator i = element.elementiterator(); i.hasnext();) {
            element e = (element) i.next();
            sbuilder.append(elementtext(e, atttext(e), lvl));
            getiterator(e, lvl);

            int count = e.nodecount();

            if (count > 0) {
                for (int j = 0; j < lvl; j++) {
                    sbuilder.append("    ");
                }
            }
            sbuilder.append("</" + e.getname() + ">\n");
        }

        return sbuilder.tostring();
    }

    /**
     * 获取当前节点的属性的值的字符串
     *
     * @description
     * @param element
     *            当前节点
     * @return
     */
    private static string atttext(element element) {

        string str = " ";
        for (int i = 0; i < element.attributecount(); i++) {
            attribute attribute = element.attribute(i);

            str += attribute.getname() + "=\"" + attribute.getvalue() + "\" ";
        }
        return str;
    }

    /**
     * 获取当前element的文本值
     *
     * @description
     * @param element
     *            当前element节点
     * @param text
     *            属性值
     * @param lvl
     *            层级
     * @return
     */
    private static string elementtext(element element, string text, int lvl) {
        string str = "";
        for (int i = 0; i < lvl; i++) {
            str += "    ";
        }
        str += "<" + element.getname();
        if (text != null && text != "") {
            str += text;
        }
     //由于dom4j里面没有 haschild这个属性或者方法,所以要用nodecount()这个方法来判断时候还有子节点
        int count = element.nodecount();
        if (count == 0) {
            return str += ">";
        }
        return str += ">\n";
    }

    /**
     *
     * @description 读取xml文件
     * @param file
     *            xml文件路径,包含文件名
     * @return document 文档
     * @throws malformedurlexception
     * @throws documentexception
     */
    public static document read(string file) throws malformedurlexception,
            documentexception {

        saxreader reader = new saxreader();
        document document = reader.read(new file(file));
        return document;
    }

    /**
     * 获取document文档的root节点
     *
     * @param document
     * @return
     */
    public static element getrootelement(document document) {
        return document.getrootelement();
    }

}

上一篇:

下一篇: