码迷,mamicode.com
首页 > 其他好文 > 详细

根据节点的绝对路径创建Xml

时间:2017-11-21 23:58:36      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:org   java   div   sys   com   style   list集合   package   roo   

根据节点的绝对路径来创建XML格式数据

package com.szzt.dataService.helper;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;

public class XMLHelper {
    private Document document;

    public XMLHelper() {

    }

    public XMLHelper(Document document) {
        super();
        this.document = document;
    }

    public Document addElement(String elementName, String value) {
        String[] nameArray = null;
        if (elementName.indexOf("/") > 0) {
            nameArray = elementName.split("/");
        }
        String s = "";
        for (int i = 0; i < nameArray.length; i++) {
            List<Node> nodes = null;
            if (i == 0) {
                nodes = document.selectNodes(nameArray[i]);
                if (nodes == null || nodes.size() == 0) {
                    Element element = document.addElement(nameArray[i]);
                    if (nameArray.length < 2) {
                        element.setText(value);
                    }
                }
                s = nameArray[i];
            } else {
                // 先判断该节点前的一个节点是不是循环节点,然后获取该节点
                Element element = null;
                if (nameArray[i - 1].indexOf("[") > -1) {
                    // 如果是循环节点则,获取改循环节点的下标
                    int index = Integer.parseInt(nameArray[i - 1].substring(nameArray[i - 1].indexOf("[") + 1,
                            nameArray[i - 1].indexOf("[") + 2));
                    element = (Element) document.selectNodes(s).get(index);
                } else {
                    element = (Element) document.selectSingleNode(s);
                }
                // 判断当前节点是不是循环节点
                Element currElement = null;
                if (nameArray[i].indexOf("[") > -1) {
                    // 如果是循环节点则判断改循环节点是否存在
                    int index = Integer.parseInt(
                            nameArray[i].substring(nameArray[i].indexOf("[") + 1, nameArray[i].indexOf("[") + 2));
                    List<Node> list = element.selectNodes(nameArray[i].substring(0, nameArray[i].indexOf("[")));
                    // 如果节点位置大于list集合的长度,则表示改节点不存在
                    if (list.size() <= index) {
                        element.addElement(nameArray[i].substring(0, nameArray[i].indexOf("[")));
                    } else {
                        // 当节点位置小list集合长度时,先判断节点存不存在
                        currElement = (Element) list.get(index);
                        if (currElement == null) {
                            currElement = element.addElement(nameArray[i].substring(0, nameArray[i].indexOf("[")));
                        }
                    }
                } else {
                    // 如果不是循环节点
                    currElement = (Element) element.selectSingleNode(nameArray[i]);
                    if (currElement == null) {
                        currElement = element.addElement(nameArray[i]);
                        if (i == nameArray.length - 1) {
                            currElement.setText(value);
                        }
                    }
                }
                if (nameArray[i].indexOf("[") > -1) {
                    s = s + "/" + nameArray[i].substring(0, nameArray[i].length() - 3);
                } else {
                    s = s + "/" + nameArray[i];
                }
            }
        }
        return document;
    }
    public Document getDocument() {
        return document;
    }
    public static void main(String[] args) {
        Document document = DocumentHelper.createDocument();
        XMLHelper xmlHelper = new XMLHelper(document);
        xmlHelper.addElement("root/accounts", "11");
         xmlHelper.addElement("root/accounts/account[0]", "");
         xmlHelper.addElement("root/accounts/account[1]", "");
         xmlHelper.addElement("root/accounts/account[0]/accountNo", "123456");
         xmlHelper.addElement("root/accounts/account[1]/password", "111111");
         xmlHelper.addElement("root/accounts/userName", "hwy");
         xmlHelper.addElement("root/accounts/account[0]/password", "000000");
         xmlHelper.addElement("root/accounts/account[1]/accountNo", "999999");
         xmlHelper.addElement("root/accounts/account[1]/password", "8888888");
        System.out.println(document.asXML());
    }
}

 

根据节点的绝对路径创建Xml

标签:org   java   div   sys   com   style   list集合   package   roo   

原文地址:http://www.cnblogs.com/hwyblog/p/7875524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!