public Document getDocument() {
		Document doc = null;

		// Retrieve result from the document builder if not set.
		this.retrieveResult();

		if (resultdoc != null) {
			doc = resultdoc;
		}
		else {
			if (resultlist != null && (queried == false)) {
				// Try to create a document from the result nodes
				try {
					JDOMFactory f = this.getFactory();
					if (f == null) { f = new DefaultJDOMFactory(); }

					doc = f.document(null);
					doc.setContent(resultlist);

					resultdoc = doc;
					resultlist = null;
				}
				catch (RuntimeException ex1) {
					// Some of the result nodes are not valid children of a
					// Document node. => return null.
					return null;
				}
			}
		}
		queried = true;

		return (doc);
	}