Home  >  Article  >  Database  >  XML解析以及增删改查的操作 2

XML解析以及增删改查的操作 2

WBOY
WBOYOriginal
2016-06-07 15:00:541072browse

我写了一个节点操作的接口INodeAction,直接贴代码: getPreviousSibling方法就是获取上一个相邻的节点,getNextSibling是获取下一个相邻的节点。 getBeifen方法主要是为了文档式化用。是根节点则返回0. printSpace方法就是根据节点辈分,在写文档的时候式化

     我写了一个节点操作的接口INodeAction,直接贴代码:


  XML解析以及增删改查的操作 2

    getPreviousSibling方法就是获取上一个相邻的节点,getNextSibling是获取下一个相邻的节点。

    getBeifen方法主要是为了文档格式化用。是根节点则返回0.

    printSpace方法就是根据节点辈分,在写文档的时候格式化

   再写了节点的基类Node:

import java.io.Serializable;
import java.util.List;
/**
 * xml文档节点基类,可以是普通节点,注释节点,文本节点
 * */
public abstract class Node implements Serializable,INodeAction{

    /**
     * 序列化版本号
     */
    private static final long serialVersionUID = 3858789701720999153L;
    /**
     * 父节点.规定 根节点的父节点为空
     * */
    protected Element father;
    /**
     * 由文档对象直接添加的节点 domFather赋值为document对象
     * */
    protected Document domFather;
    @Override
    public Integer getBeifen(){
        Integer n;
        if(father==null){
            n = 0;
            return n;
        }
        Node node = this;
        n = 0;
        while(node.father!=null){
            n++;
            node = node.father;
            if(node.father==null){
                return n;
            }
        }
        if(!(node.father==null)){
            return null;
        }
        return n;
    }
    
    public Element getFather() {
        return father;
    }
    @Override
    public String printSpace() {
        StringBuffer sb = new StringBuffer();
        Integer n = getBeifen();
        if (n == null) {
            return sb.toString();
        }
        for (int i = 0; i  nodeList = domFather.getNodeList();
                int idx = nodeList.indexOf(this);
                return idx==0?null:nodeList.get(idx-1);
            }
        }
        List nodeList = father.getNodeList();
        int idx = nodeList.indexOf(this);
        return idx==0?null:nodeList.get(idx-1);
    }
    
    @Override
    public Node getNextSibling() {
        if(father==null){
            if(domFather==null){
                DocumentUtil.throwException("节点还没添加到文档!");
            }else{
                List nodeList = domFather.getNodeList();
                int idx = nodeList.indexOf(this);
                return idx==nodeList.size()-1?null:nodeList.get(idx+1);
            }
        }
        List nodeList = father.getNodeList();
        int idx = nodeList.indexOf(this);
        return idx==nodeList.size()-1?null:nodeList.get(idx+1);
    }
}


关键在于编程的思想。有想法就什么都能实现。下面就是写节点的具体实现类了。另起一篇。







Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:2dx 分辨率Next article:详解Cocos2d-X中宏CC