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

📄 tcuxmlvectorlists.java

📁 为公司做的质量考核接口源码,用spring,hibernate,XML实现,对XML接口编程很有帮助
💻 JAVA
字号:
/**
 * 
 */
package com.jr81.source.xml.source;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 * @author Administrator
 *
 */
public class TcuXmlVectorLists implements TcuInterfaceXmlLists {
	private Vector Lists = new Vector();
	private String XmlHead = "<?xml version='1.0' encoding = 'GB2312'?> ";
	private String Xml1 = "<Lists>";
	private String Xml2 = "</Lists>";
	
	public TcuXmlVectorLists() {
		
	}
	
	public void Clear() {
		Lists.clear();
	}
	
	public int GetCount() {
		return Lists.size();
	}
	
	public int GetLength() {
		int Length = Xml1.length() + Xml2.length();
		for (int i = 0; i < GetCount(); i++) {
			Length += GetList(i).GetLength();
		}
		return Length;
	}
	
	public void AddList(TcuXmlList value) {
		Lists.add(value);
	}
	
	public void AddList(byte[] name, byte[] value) {
		TcuXmlList newList = new TcuXmlList();
		newList.setName(name);
		newList.setValue(value);
		Lists.add(newList);
	}
	
	public void AddList(byte[] name, byte[] value, int type) {
		// TODO Auto-generated method stub
		if (type == 0) {//default
			TcuXmlList newList = new TcuXmlList();
			newList.setName(name);
			newList.setValue(value);
			AddList(newList);
		} else if (type == 1) {//Base64
			TcuXmlList newList = new TcuXmlList();
			newList.setName(name);
			newList.setBase64Value(value);
			AddList(newList);
		} else if (type == 2) {//Compress
			TcuXmlList newList = new TcuXmlList();
			newList.setName(name);
			newList.setCompressValue(value);
			AddList(newList);
		} else if (type == 3) {//CompressBase64
			TcuXmlList newList = new TcuXmlList();
			newList.setName(name);
			newList.setCompressBase64Value(value);
			AddList(newList);
		} else {//default
			TcuXmlList newList = new TcuXmlList();
			newList.setName(name);
			newList.setValue(value);
			AddList(newList);
		}
	}
	
	public void DeleteList(int index) {
		Lists.remove(index);
	}
	
	public void DeleteList(String name) {
		// TODO Auto-generated method stub
		
	}
	
	public TcuXmlList GetList(int index) {
		return (TcuXmlList)Lists.elementAt(index);
	}
	
	public TcuXmlList GetList(String name) {
		for (int i = 0; i < GetCount(); i++) {
			if (GetList(i).getName().equals(name)) {
				return GetList(i);
			}
		}
		return null;
	}
	
	public byte[] GetListByteValue (String name) {
		for (int i = 0; i < GetCount(); i++) {
			if (GetList(i).getName().equals(name)) {
				return GetList(i).getValue();
			}
		}
		return null;
	}
	
	public String GetListStrValue(String name) {
		
		for (int i = 0; i < GetCount(); i++) {
			if (GetList(i).getName().equals(name)) {
				byte[] tmpValue = GetList(i).getValue();
				if (tmpValue != null) {
				String strResult = new  String(tmpValue);
				return strResult;
				} else {
					return "";
				}
			}
		}
		return "";
	}
	
	public void Assign(TcuLists value) {
		Clear();
		for (int i = 0; i < value.GetCount(); i++) {
			TcuXmlList tmpList = new TcuXmlList();
			tmpList.Assign(value.GetList(i));
			AddList(tmpList);
		}
	}
	
	public boolean FromByteXml(byte[] value) {
		Clear();
		SAXBuilder sb = new SAXBuilder();
		Document MyDocument = new Document();
		
		ByteArrayOutputStream Output = new ByteArrayOutputStream(XmlHead.length() + value.length);
		Output.write(XmlHead.getBytes(),0, XmlHead.length());
		Output.write(value,0,value.length);
		ByteArrayInputStream InputStream = new ByteArrayInputStream(Output.toByteArray());
		
		try {
			MyDocument=sb.build(InputStream);
			
			Element root = MyDocument.getRootElement();
			List ChildrenList = root.getChildren("List");
			
			Iterator i = ChildrenList.iterator();
			while(i.hasNext()) {
				Element List1 = (Element)i.next();
				TcuXmlList tmpList = new TcuXmlList();
				tmpList.setName(List1.getChildText("Name").getBytes());
				tmpList.setValue(List1.getChildText("Value").getBytes());
				AddList(tmpList);
			}
			
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	public byte[] ToByteXml() throws IOException {
		
		
		ByteArrayOutputStream value = new ByteArrayOutputStream(GetLength());
		value.write(Xml1.getBytes(), 0, Xml1.length());
		for (int i = 0; i < GetCount(); i++) {
			TcuXmlList tmpList = GetList(i);
			value.write(tmpList.ToByteXml(), 0, tmpList.GetLength());
		}
		value.write(Xml2.getBytes(), 0, Xml2.length());
		/*String XmlText = "<Lists>";
			for (int i = 0; i < GetCount(); i++) {
				//XmlText += GetList(i).ToByteXml();
			}
		XmlText += "</Lists>";
		return XmlText;
		*/
		
		return value.toByteArray();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public void AddList(byte[] name, boolean value) {
		// TODO Auto-generated method stub
		
	}

	public void AddList(byte[] name, int value) {
		// TODO Auto-generated method stub
		
	}

	public void AddList(byte[] name, float value) {
		// TODO Auto-generated method stub
		
	}

	public boolean GetListBooleanValue(String name) {
		// TODO Auto-generated method stub
		return false;
	}

	public float GetListFloatValue(String name) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int GetListIntValue(String name) {
		// TODO Auto-generated method stub
		return 0;
	}

	

	

}

⌨️ 快捷键说明

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