class KVMessage implements Serializable {

	public String toXML() throws KVException {
		if (msgType == null || (msgType.equals("delreq") && key == null)
				|| (msgType.equals("putreq") && (value == null || key == null))
				|| (msgType.equals("resp") && key != null && value == null)) {
			throw new KVException(new KVMessage("resp",
					"Message format incorrect"));
		}

		DocumentBuilder db = null;
		try {
			db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			throw new KVException(new KVMessage("resp",
					"Message format incorrect"));
		}
		Document doc = db.newDocument();
		Element kv = doc.createElement("KVMessage");
		Element keey, valoo, massage, toy;
		kv.setAttribute("type", msgType);
		if (key != null) {
			keey = doc.createElement("Key");
			keey.setTextContent(key);
			kv.appendChild(keey);
		}
		if (value != null) {
			valoo = doc.createElement("Value");
			valoo.setTextContent(value);
			kv.appendChild(valoo);
		}
		if (message != null) {
			massage = doc.createElement("Message");
			massage.setTextContent(message);
			kv.appendChild(massage);
		}
		if (tpcOpId != null) {
			toy = doc.createElement("TPCOpId");
			toy.setTextContent(tpcOpId);
			kv.appendChild(toy);
		}
		doc.appendChild(kv);
		duck = doc;
		// adapted from
		// http://stackoverflow.com/questions/5456680/xml-document-to-string
		doc.setXmlStandalone(true);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = null;
		try {
			transformer = tf.newTransformer();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		}
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");

		StringWriter writer = new StringWriter();
		try {
			transformer.transform(new DOMSource(doc), new StreamResult(writer));
		} catch (TransformerException e) {
			e.printStackTrace();
		}
		String output = writer.getBuffer().toString();
		return output;
	}

	public  KVMessage(KVMessage kvm);
	public  KVMessage(String msgType);
	public  KVMessage(String msgType, String message);
	public  KVMessage(Socket sock);
	public  KVMessage(Socket sock, int timeout);

}