• 技术文章 >数据库 >mysql教程

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

    2016-06-07 15:00:54原创468

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

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



    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 < n; i++) {
                sb.append("  ");
            }
            return sb.toString();
        }
        
        public Document getDomFather() {
            return domFather;
        }
        
        @Override
        public Node getPreviousSibling(){
            if(father==null){
                if(domFather==null){
                    DocumentUtil.throwException("节点还没添加到文档!");
                }else{
                    List 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);
        }
    }


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

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:2dx 分辨率 下一篇:详解Cocos2d-X中宏CC

    相关文章推荐

    • 实例讲解MySQL与InnoDB下共享锁与排他锁• mysql怎样修改用户• mysql中怎么调用存储过程• mysql怎样增加表格• 如何解决ubuntu mysql 乱码问题

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网