⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tag.java

📁 短信网关发送接受平台。
💻 JAVA
字号:
package XML;
import java.util.*;

public class Tag{
	private String tagName;
	//private Vector subTags=new Vector();
	private Vector elements=new Vector();
	private Vector attributes=new Vector();
	
	public Tag(){
		this.tagName="";
	}
	
	public Tag(String tagName){
		this.tagName=tagName;
	}
	
	public void setTagName(String tagName){
		this.tagName=tagName;
	}
	
	public String getTagName(){
		return this.tagName;
	}
	
	public void add(Tag subTag){
		elements.add(subTag);
	}
	
	public void add(Element element){
		elements.add(element);
	}
	
	public void add(Attribute attribute){
		attributes.add(attribute);
	}
	
	public String toString(){
		String xmlStr="";
		Attribute att;
		Element ele;
		Tag subTag;
		if (isValid()){
			xmlStr="<"+this.tagName;
			for (int i=0;i<this.attributes.size();i++){
				att=(Attribute)attributes.elementAt(i);
				xmlStr+=" "+att;
			}
			xmlStr+=">";
		//del	for (int i=0;i<this.subTags.size();i++){
		//del		subT=(Tag)subTags.elementAt(i);
		//del		xmlStr+=subT;
		//del	}
			for (int i=0;i<this.elements.size();i++){
				if (elements.elementAt(i) instanceof Tag){
					subTag=(Tag)elements.elementAt(i);
					xmlStr+=subTag;
				}else{				
					ele=(Element)elements.elementAt(i);
					xmlStr+=ele;
				}
			}
			xmlStr+="</"+this.tagName+">";
		}
		return xmlStr;
	}
	
	public boolean isValid(){
		if (this.tagName==null){
			return false;
		}
		return true;
	}
	
	public static void main(String s[]){
		Tag root=new Tag("packet");
		Attribute ip=new Attribute("ip","192.168.0.1");
		root.add(ip);
			Attribute user=new Attribute("UID","dongyu");
		root.add(user);
			Attribute pwd=new Attribute("PWD","hi");
		root.add(pwd);
			Element command=new Element("command","submit");
		root.add(command);
			Element dest=new Element("dest_terminal_id","13910139946");
		root.add(dest);	
			Element src=new Element("src_terminal_id","1860");
		root.add(src);
			Tag msg=new Tag("message");
				Attribute msg_fmt=new Attribute("msg_fmt",15);
			msg.add(msg_fmt);
				Element msg_content=new Element("msg_content","hi,测试!!!");
			msg.add(msg_content);
		root.add(msg);
		System.out.println(root);
	}
}		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -