public static Element parse(final String xml) throws DocumentException
    {
        String toParse = xml.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "").trim();

        // Verify if xml ends with close tag that matches the first tag.
        final Matcher matcher = Pattern.compile("[A-Za-z:]+").matcher(toParse);
        if (matcher.find()) {
            final String fakeEndTag = "</" + matcher.group() + ">";
            final String emptyElementTagPattern = "<" + matcher.group() + "[^/>]*/>";
            if (!toParse.trim().endsWith(fakeEndTag) && !Pattern.compile(emptyElementTagPattern).matcher(toParse).find()) {
                toParse += fakeEndTag;
            }
        }
        return DocumentHelper.parseText(toParse).getRootElement();
    }
Parses text as an XML element.

When the provided input is an element that is not closed, then a closing element is automatically generated. This
helps to parse `stream` elements, that are closed only when the XMPP session ends.

@param xml The data to parse
@return an XML element
xmppserver/src/test/java/org/jivesoftware/openfire/session/AbstractRemoteServerDummy.java