class DOMParserUtil {

    public static Node createNodeAtPosition(Node containerNode, String nodeToCreateName, String nodeContent, Integer position) {
        Node toReturn = containerNode.getOwnerDocument().createElement(nodeToCreateName);
        if (nodeContent != null) {
            toReturn.setTextContent(nodeContent);
        }
        if (containerNode.hasChildNodes() && position != null && position < containerNode.getChildNodes().getLength()) {
            Node positionNode = containerNode.getChildNodes().item(position);
            containerNode.insertBefore(toReturn, positionNode);
        } else {
            containerNode.appendChild(toReturn);
        }
        return toReturn;
    }

    private  DOMParserUtil();

}