Artículo

Fecha: 2010-03-24 16:41:07

Convert Axis Java Object to XML


Util class to convert a Axis Java Object to XML:
(very useful to debug)
________________________________________________________

public class AxisObjectUtil {
	/**
	 * Convert an Axis data object (as generated by WSDL2Java) to an XML string.
	 * 
	 * @param obj
	 *            The xis JavaBean object.
	 * @param removeNamespaces
	 *            If true all namespace attributes will be removed from the
	 *            returned XML string.
	 * 
	 * @return A string containing the XML of the seriaslized Axis JavaBean
	 *         object.
	 * @throws AxisObjectException
	 *             If an error is encountered while serializing the Axis
	 *             JavaBean.
	 */
	public static String serializeAxisObject(final Object obj,
			final boolean removeNamespaces, final boolean prettyPrint)
			throws Exception {
		final StringWriter outStr = new StringWriter();
		final TypeDesc typeDesc = getAxisTypeDesc(obj);
		QName qname = typeDesc.getXmlType();
		String lname = qname.getLocalPart();
		if (lname.startsWith(">") && lname.length() > 1)
			lname = lname.substring(1);

		qname = removeNamespaces ? new QName(lname) : new QName(qname
				.getNamespaceURI(), lname);
		final AxisServer server = new AxisServer();
		final BeanSerializer ser = new BeanSerializer(obj.getClass(), qname,
				typeDesc);
		SerializationContext ctx = new SerializationContext(outStr,
				new MessageContext(server));
		ctx.setSendDecl(false);
		ctx.setDoMultiRefs(false);
		ctx.setPretty(prettyPrint);
		try {
			ser.serialize(qname, new AttributesImpl(), obj, ctx);
		} catch (final Exception e) {
			throw new Exception("Unable to serialize object "
					+ obj.getClass().getName(), e);
		}

		String xml = outStr.toString();

		if (removeNamespaces) {
			// remove any namespace attributes
			xml = xml.replaceAll(" xmlns[:=].*?".*?"", "").replaceAll(
					" xsi:type=".*?"", "");
		}

		return (xml);
	}

	/**
	 * Return the Axis TypeDesc object for the passed Axis JavaBean.
	 * 
	 * @param obj
	 *            The Axis JavaBean object.
	 * 
	 * @return The Axis TypeDesc for the JavaBean.
	 * @throws AxisObjectException
	 *             If the passed object is not an Axis JavaBean.
	 */
	public static TypeDesc getAxisTypeDesc(final Object obj) throws Exception {
		final Class objClass = obj.getClass();
		try {
			final Method methodGetTypeDesc = objClass.getMethod("getTypeDesc",
					new Class[] {});
			final TypeDesc typeDesc = (TypeDesc) methodGetTypeDesc.invoke(obj,
					new Object[] {});
			return (typeDesc);
		} catch (final Exception e) {
			throw new Exception("Unable to get Axis TypeDesc for "
					+ objClass.getName(), e);
		}
	}
}


________________________________________________________

Autor:Jose Miguel Bataller

Buscador

Buscar


Últimas entradas

Preseleccionar un option de un element select html con jQuery »

Testing for Empty elements in XSL »

Convert XML File to String »

Java Keytool Commands for Managing security certificates »

Execute servlet filter before calling Axis web service »

Set trustStore for SSL connection in Java »

Example of a client-wsdd.config »

Convert Axis Java Object to XML »

Validating a String using Regex in Java »

Open browser in Java »