📄 node.java
字号:
/** * Copyright 2004 Carlos Silva A. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package com.csa.lib.xml.dom;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.StringTokenizer;import java.util.Vector;import org.xml.sax.AttributeList;/** * Node implementa un arbol tipo DOM pero NO es DOM. * No soporta contenidos mezclados (por ejemplo) ni sub esquemas. * * <P>Se uso para utilizar un parser SAX para leer los contenidos de * los archivos XML sin usar Xerces.</p> * * <P>Esta basado en MinML (ver licencia en META-INF/)</p> * * <p>$Date: 2005/09/16 13:43:44 $</p> * @version $Revision: 1.3 $ * @author Carlos Silva */public class Node { String name; StringBuffer value; Hashtable attributes; Hashtable attributeTypes; Vector childs; Node parent = null; Node next = null; Node prev = null; public Node (String name) { this.name = name; } public Node (String name,AttributeList attributes) { this.name = name; if (attributes.getLength()>0) { this.attributes = new Hashtable(); this.attributeTypes = new Hashtable(); for (int i = 0; i < attributes.getLength(); i++) { this.attributes.put(attributes.getName(i), attributes.getValue(i)); this.attributeTypes.put(attributes.getName(i), attributes.getType(i)); } } } public Node (String name,Hashtable attributes) { this.name = name; this.attributes = attributes; } public Node (String name,String value) { this.name = name; if (value!=null) this.value = new StringBuffer(value); } public void setAttribute(String name, String value) { if (attributes==null) attributes= new Hashtable(); attributes.put(name,value); } public void addValue(String s){ if (value==null) value = new StringBuffer(); value.append(s); } public String getValue(){ if (value==null) return null; return value.toString(); } public void appendChild(Node child){ if (childs==null) childs=new Vector(); child.parent=this; child.next=null; if (childs.size()>0) { Node prev = (Node) childs.lastElement(); child.prev= prev; prev.next=child; } childs.add(child); } public void appendChild(String name, String value){ appendChild(new Node(name, value)); } public Iterator getChildElements(){ if (childs==null) return (new Vector()).iterator(); return childs.iterator(); } public String getNodeName(){ return name; } public Node getParentNode(){ return parent; } public Node getNextSibling(){ return next; } public Node getPrevSibling(){ return prev; } public Node getFirstChild(){ if (childs==null) return null; return (Node)childs.firstElement(); } public String getAttribute(String n){ if (attributes==null) return null; return (String) attributes.get(n); } Node getChildByName(String name) { for (Iterator i = getChildElements(); i.hasNext();) { Node c = (Node) i.next(); if (name.equals(c.getNodeName())) { return c; } } return null; } String getChildText(String name) { Node n = getChildByName(name); if (n == null) return null; return n.getValue(); } Node getElementByPath(String path) { StringTokenizer st = new StringTokenizer(path, "/"); Node n = this; while (st.hasMoreTokens()) { String t = st.nextToken(); n = n.getChildByName(t); if (n == null) return null; } return n; } String getElementText(String path) { Node n = getElementByPath(path); return n.getValue(); } public void show() { show(" "); } void show(String pre) { System.out.println(pre+name+": " +getValue()); if (attributes!=null) for (Enumeration e = attributes.keys(); e.hasMoreElements();) { String key = (String) e.nextElement(); String value = (String) attributes.get(key); System.out.println(pre+" @" + key + "= \"" + value + "\""); } if (childs!=null) for (int i =0; i<childs.size();i++){ ((Node) childs.get(i)).show(pre+" "); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -