package survey.utils;

import org.w3c.dom.Element;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.OutputFormat;
import java.io.IOException;

public class XmlUtil {

    /* Serializes an Element, and returns it back in form of string.
     * @param element  The element to serialize
     * @param maxLen   The max length that the resulting string can have
     * @return         The string serialized,
     * @throws IOException If the serialization takes more than maxLen characters
     */
    public static String getSerializedXMlElementString(Element element, int maxLen) throws IOException {
		OutputStreamStringBuffer buffer = new OutputStreamStringBuffer(maxLen);

		OutputFormat format = new OutputFormat();
		format.setOmitXMLDeclaration(true);
		XMLSerializer serializer = new XMLSerializer(buffer, format);
		serializer.serialize(element);
		String text = buffer.toString();

		return text;
    }

}

