class AxiomUtils {

	public static Document toDocument(SOAPEnvelope envelope) {
		try {
			if (envelope instanceof Element) {
				return ((Element) envelope).getOwnerDocument();
			} else {
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				envelope.build();
				envelope.serialize(bos);

				ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
				DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactoryUtils.newInstance();
				documentBuilderFactory.setNamespaceAware(true);
				return documentBuilderFactory.newDocumentBuilder().parse(bis);
			}
		} catch (Exception ex) {
			throw new IllegalArgumentException("Error in converting SOAP Envelope to Document", ex);
		}
	}

}