/** 序列化整颗树 */ public String elementToString() { StringBuffer sbu = new StringBuffer(); if ("".equals(tagName)) { if (contents != null) { sbu.append(contents); } } else { sbu.append("<" + tagName + " "); if (attributesName != null) { for (int i = 0; i < attributesName.length; i++) { sbu.append(attributesName[i] + "=\"" + attributesValue[i] + "\" "); } } if (children.size() == 0) { sbu.append("/>"); } else { sbu.append(">"); for (int i = 0; i < children.size(); i++) { BXmlElement elt = (BXmlElement) children.elementAt(i); sbu.append(elt.elementToString()); } sbu.append("</" + tagName + ">"); } } return sbu.toString(); }
/** 打印以此element为root的整颗树 */ public void print(int indentNum) { for (int i = 0; i < indentNum; i++) { // System.out.print("____"); } if ("".equals(tagName)) { if (contents != null) { System.out.println(contents); } } else { // System.out.print("<" + tagName + " "); if (attributesName != null) { for (int i = 0; i < attributesName.length; i++) { // System.out.print(attributesName[i] + "=\"" + attributesValue[i] + "\" "); } } if (children.size() == 0) { System.out.println("/>"); } else { System.out.println(">"); for (int i = 0; i < children.size(); i++) { BXmlElement elt = (BXmlElement) children.elementAt(i); elt.print(indentNum + 1); } for (int i = 0; i < indentNum; i++) { // System.out.print("____"); } System.out.println("</" + tagName + ">"); } } }
/** * 判断以自己为root的树,是否有此节点 * * @param Element * @return boolean */ public boolean isMyChildren(BXmlElement Element) { int size = this.children.size(); for (int i = 0; i < size; i++) { BXmlElement ele = (BXmlElement) this.children.elementAt(i); if (ele.equals(Element)) { return true; } int childrensize = ele.children.size(); if (childrensize > 0) { for (int k = 0; k < childrensize; k++) { BXmlElement elex = (BXmlElement) ele.children.elementAt(k); boolean istrue = elex.isMyChildren(Element); if (istrue == true) { return true; } else { // 继续运行 } } } } return false; }
/** 打印以此element为root的整颗树 */ public void printNode(int indentNum) { for (int i = 0; i < indentNum; i++) { // System.out.print(" "); } if ("".equals(tagName) && contents != null) { System.out.println(contents); } else { // System.out.print("└──<" + tagName + "> "); if (attributesName != null) { for (int i = 0; i < attributesName.length; i++) { // System.out.print(attributesName[i] + "=\"" + attributesValue[i] + "\", "); } } System.out.println(""); if (children.size() > 0) { for (int i = 0; i < children.size(); i++) { BXmlElement element = (BXmlElement) children.elementAt(i); element.printNode(indentNum + 1); } } } }