[xmlparser]Code-Node

此博客展示了Java代码,定义了一个名为Node的类用于处理XML节点。该类包含节点类型、子节点列表、属性列表和节点值等属性,还提供了设置和获取这些属性的方法,以及添加属性、获取属性值等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.xml;

import java.util.ArrayList;
import java.util.List;

public class Node {

    public final static int NODE_UNKNOW=-1;    
    public final static int NODE_XML=0;
    public final static int NODE_DOCTYPE=1;
    public final static int NODE_TAG=2;
    public final static int NODE_TEXT=3;
    public final static int NODE_REMARK=4;
    public final static int NODE_CDATA=5;
    
    private int type;
    private List childs=new ArrayList();
    private AttributeList attributes=new AttributeList();
    private String value;

    public Node() {}

    public void setType(int type) {
        this.type = type;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void addAttribute(Attribute att) {
        attributes.addAttribute(att);
    }

    public int getType() {
        return type;
    }


    public AttributeList getAttributes() {
        return attributes;
    }

    public String getValue() {
        return value;
    }

    public String toString(){
        return "#"+type+" "+ value;
    }

    public String getAttributeValue(String name){
        Attribute att;
        for(int i=0,n=attributes.size();i<n;i++){
            att=attributes.getAttribute(i);
            if(name.equals(att.getName())){
                return att.getValue();
            }
        }
        return null;
    }

    public void addChild(Node node){
        childs.add(node);
    }
    public Node getChild(int i){
        return (Node)childs.get(i);
    }
    
    
    public void dump(int level){
        StringBuffer sb=new StringBuffer();
//        for(int i=0;i<level;i++){
//            sb.append('/t');
//        }
        sb.append("Level=").append(level).append(" [type=").append(type).append(']').append(value);
        if(attributes.size()>0){
            sb.append("/n/t/tAttributes: ").append(attributes.getAttributeList());
        }
        System.out.println(sb);
        for(int i=0,n=childs.size();i<n;i++){
            getChild(i).dump(level+1);   
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值