📄 element.java
字号:
package com.zcsoft.xml;import java.util.*;/** 标记/成员 */public class Element implements java.io.Serializable{ /** 将元素间的文本作为一种元素对象时,则对应该元素对象的标记名称为"#text" */ public static final String TAG_TEXT = "#text"; /** 将元素间的注释作为一种元素对象时,则对应该元素对象的标记名称为"#comment" */ public static final String TAG_COMMENT = "#comment"; /** 所属父标记。对于XML的根节点,总是null */ private Element parent; /** 标记名。对于Character/Text,标记名为<code>"#text"</code> */ private String tagName; /** 标记值。仅对Character/Text、Notation、ProcessingInstruction、Comment有效 */ private String tagValue; /** 子节点标记。 */ private ArrayList children; /** 标记中定义的属性 */ private ArrayList attriutes; public Element(){} public Element(String tagName) { this.tagName = tagName; } public int getChildrenCount() { return (children != null?children.size():0); } public Iterator getChildren() { return this.children.iterator(); } /** * 对于可能有多个同一标记的子元素的获取方法 * @param tagName 子元素的标记名称 * @return 不为null的Iterator */ public Iterator getChildren(String tagName) { List sub = new LinkedList(); storeChildren(tagName, sub); return sub.iterator(); } /** * 对于可能有多个同一标记的子元素的获取方法。 * 将子元素存入指定的列表中。 * @param tagName 子元素的标记名称 * @param childrenStore 存储子元素的列表实例 * @exception childrenStore = null */ public void storeChildren(String tagName, List childrenStore) { if (tagName != null) { int n = this.getChildrenCount(); for (int i = 0; i < n; i++) { Element child = (Element)children.get(i); if ( tagName.equals(child.tagName) ) { childrenStore.add(child); } } } } /** * 对于最多只有一个子元素的情况下获得该子元素的方便方法。 * @param tagName * @return 如果没有定义,则返回null。 */ public Element getChild(String tagName) { if (tagName != null && children != null) { for (int i = 0; i < children.size(); i++) { Element child = (Element)children.get(i); if ( tagName.equals(child.tagName) ) { return child; } } } return null; } public void addChild(Element e) { if (e == null) { throw new NullPointerException(); } if (this.children == null) { this.children = new ArrayList(2); } this.children.add(e); e.setParent( this ); }//// public void removeChild(Element e)// {//// }//// public void removeChildByName(String tagName)// {//// }// public int getAttributesCount() { return this.attriutes != null?this.attriutes.size():0; } /** * 获取指定属性的值 * @param name 属性的名称 * @return 如果指定名称的属性未被定义,则返回null。 */ public String getAttributeValue(String name) { int index = this.indexOfAttribute(name); return index >= 0?this.getAttribute(index).getValue():null; } /** * 获取指定属性的在其属性集合中的位置索引 * @param name 属性的名称 * @return 如果指定名称的属性未被定义,则返回-1,否则返回非负数。 */ public int indexOfAttribute(String name) { if (name != null && attriutes != null) { for (int i = 0; i < attriutes.size(); i++) { Attribute attr = (Attribute)attriutes.get(i); if ( name.equals(attr.getName()) ) { return i; } } } return -1; } public boolean hasAttribute(String name) { return this.indexOfAttribute(name) != -1; } public Attribute getAttribute(int i) { return (Attribute)this.attriutes.get(i); } public void addAttibute(Attribute a) { if (a == null) { throw new NullPointerException(); } if (this.attriutes == null) { this.attriutes = new ArrayList(2); } this.attriutes.add(a); } /** * 通知Element的属性个数。通常情况下,XML分析器在分析标记时,能确切知道属性的个数。 * 为提高性能增加的方法 * */ public void encureAttributesCount(int cnt) { if (cnt <= 0) { this.attriutes = null; return ; } if (this.attriutes == null) { this.attriutes = new ArrayList(cnt); } else { this.attriutes.ensureCapacity(cnt); } }//// public void removeAttribute(Attribute name)// {//// }//// public void removeAttributeByName(String name)// {//// } /** @tagName标记名。对于Character/Text,标记名为<code>"#text"</code> */ public void setTagName(String tagName) { this.tagName = tagName; } public String getTagName() { return tagName; } public void setParent(Element parent) { this.parent = parent; } public Element getParent() { return parent; } public void setTagValue(String tagValue) { this.tagValue = tagValue; } public String getTagValue() { return tagValue; } public String toString() { return String.valueOf(tagName); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -